Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



RESIZE IMAGES WITH VFP9 and GDI+

Resize Image with gdiplus
The function GetThumbnailImage can be used to get a resized image, but the result may come with less quality.
 
The example below does the same thing, but creates an image with a better quality.
 
#DEFINE GDIPLUS_PIXELFORMAT_1bppIndexed      0x00030101
#DEFINE GDIPLUS_PIXELFORMAT_4bppIndexed      0x00030402
#DEFINE GDIPLUS_PIXELFORMAT_8bppIndexed      0x00030803
#DEFINE GDIPLUS_PIXELFORMAT_16bppGrayScale   0x00101004
#DEFINE GDIPLUS_PIXELFORMAT_16bppRGB555      0x00021005
#DEFINE GDIPLUS_PIXELFORMAT_16bppRGB565      0x00021006
#DEFINE GDIPLUS_PIXELFORMAT_16bppARGB1555    0x00061007
#DEFINE GDIPLUS_PIXELFORMAT_24bppRGB         0x00021808
#DEFINE GDIPLUS_PIXELFORMAT_32bppRGB         0x00022009
#DEFINE GDIPLUS_PIXELFORMAT_32bppARGB        0x0026200A
#DEFINE GDIPLUS_PIXELFORMAT_32bppPARGB       0x000E200B
#DEFINE GDIPLUS_PIXELFORMAT_48bppRGB         0x0010300C
#DEFINE GDIPLUS_PIXELFORMAT_64bppPARGB       0x001C400E

lcSource = GETPICT("jpg;gif;bmp")
lcDestination = 
ADDBS(JUSTPATH(lcSource))+ "Resized_" +;
   JUSTSTEM(lcSource)+".bmp"
LOCAL loImage AS GpImage OF ffc/_gdiplus.vcx
loImage = NEWOBJECT("GpImage", HOME() + "ffc/_gdiplus.vcx")
loImage.CreateFromFile(lcSource)
 
LOCAL loBitmap AS GpBitmap OF ffc/_gdiplus.vcx
loBitmap = NEWOBJECT("GpBitmap", HOME() + "ffc/_gdiplus.vcx")
LOCAL loGraphics as GpGraphics OF HOME() + ffc/_gdiplus.vcx
loGraphics = NEWOBJECT('GpGraphics',HOME() + 'ffc/_gdiplus.vcx')
 
*** Now we create a new image with
*** Create Method - Creates a bitmap object.
*** Syntax: ? THIS.Create(tnWidth, tnHeight[, tnPixelFormat])
***
*** tnPixelFormat, optional, one of GDIPLUS_PIXELFORMAT_* constants,
*** defaults to GDIPLUS_PIXELFORMAT_32bppARGB.
 
LOCAL lnNewWidth, lnNewHeight
lnNewWidth = 640  && Put here the desired Width
lnNewHeight = 480 && Put here the desired Height
 
loBitmap.Create(lnNewWidth, lnNewHeight, GDIPLUS_PIXELFORMAT_32bppPARGB)
*** The other constants are in the beginning of this code
 
loGraphics.CreateFromImage(loBitmap)
loGraphics.DrawImageScaled(loImage, 0, 0, lnNewWidth, lnNewHeight)
loBitmap.SaveToFile(lcDestination, "image/bmp")
RETURN
 
 
UPDATE 04/08/2007
 
Below is another way to resize an image using the GetThumbnailImage function, shown by Craig Boyd in a UT thread:
 
LOCAL lcSource, lcDestination
lcSource = GETPICT()
lcDestination = "C:\" + JUSTFNAME(lcSource) && Image will be saved on to C:\
IF !CreateThumbNail(lcSource, lcDestination, 85, 100)
	MESSAGEBOX("Unable to create image")
ENDIF

FUNCTION CreateThumbnail(tcSource, tcDestination, tnWidth, tnHeight)
	LOCAL llSuccess, loImage, loThumbImage
	llSuccess = .F.
	IF FILE(tcSource)
		loImage = Newobject('GpImage',HOME(1) + 'FFC\_gdiplus.vcx')
		loImage.CreateFromFile(tcSource)
		loThumbImage = loImage.GetThumbnailImage(tnWidth, tnHeight)
		IF TYPE("loThumbImage") = "O"
			llSuccess = loThumbImage.SaveToFile(tcDestination, "image/jpeg")
		ENDIF
	ENDIF
	RETURN llSuccess
ENDFUNC
Published Monday, February 06, 2006 6:00 PM by cesarchalom
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Vassilis said:

That's really cool. Thank you Caesar.

Just a simple bug. -:)
instead of :
lcDestination = "Resized_" + lcSource

it works better with:
lcDestination = ADDBS(JUSTPATH(lcSource))+"Resized_" + JUSTSTEM(lcSource)+".bmp"

BTW, do you know how you can skew an image?
February 9, 2006 12:03 PM
 

Cesar said:

Hi Vassilis,
Thanks very much for the feedback and fix.
I've already fixed the code.

What do you mean by "skew an image" ?
Can you send me a link with an example ?
Sorry for my bad english.
I'm from Brazil.
February 9, 2006 1:02 PM
 

Vassilis said:

Hi Cesar,

Try : WinKey + R --> mspaint --> load an image and press Ctrl + W.
You will see settings for the SKEW effect in the second group of textboxes.

Thank you
February 9, 2006 2:35 PM
 

Cesar said:

Thanks,
Yes, this can be done too.
I'll try to post an example soon !
Regards
Cesar
February 9, 2006 2:51 PM
 

Cesar said:

February 9, 2006 5:46 PM
 

Cesar Chalom said:

May 24, 2006 5:38 PM
 

Cesar Chalom said:

May 24, 2006 5:39 PM
 

Siegfried Dolleisch said:

Hi Cesar,

the created pictures have a resolution of 96 dpi, but I need 72 dpi.
How can I change it?

Greetings from Bavaria

Siegfried
September 6, 2006 9:20 AM
 

Alec said:

Hi Cesar,

Hi Alec,

Thanks for the sample code. Very helpful for making thumbnails.  Two questions:

#1 Is there a way to do this with a string of the image rather than working with files?  I am pulling the image from a database field and forwarding it via IP and it would seem that writing the image out to a file on disk will slow the process down considerably.

YES, it is possible, and I hope to Blog about it soon. Anyway, the speed will depend on the quantity of strings you need to process. You can have a look at the Gdiplus-X classes too. Bo Durban has prepared some cool usages of images without disk access.

#2 Is there a way to determine the size attributes of the source image so as to maintain the proper aspect ratio when thumbnailing it?  If Idon't know the height and width of the source image then arbitrarily changing it may (and most likely will )change the aspect ration and make the image apear funny...

Yes, that's very easy too. Pls check this link: GETTING IMAGE PROPERTIES WITH GDI+

Thanks

Alec

HTH

Cesar

October 8, 2006 8:03 AM
 

Alec Gagne said:

Hi Cesar,

Greatpost. Thanks!

When using the _gdiplus class is it possible to pass and receive an image as a variable rather than going to/from disk.  I want to pull image from a table and pass it to another program without having to go to disk to thumbnail the image. If so, how?

Thanks

Alec
October 15, 2006 8:17 AM
 

Jürgen said:

Thank you
October 19, 2006 5:49 PM
 

Ana María Bisbé said:

Versión en Español de este artículo in / Spanish version at http://www.portalfox.com/article.php?sid=2170  
July 24, 2007 12:12 AM
 

foxpro.catalyst ?? » Blog Archive » More VFP9 and GDIs said:

October 8, 2007 10:11 PM

What do you think?

(required) 
(optional)
(required)