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)
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