2006-02-06

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

11 comments:

  1. 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?

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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

    ReplyDelete
  4. Thanks,

    Yes, this can be done too.

    I'll try to post an example soon !

    Regards

    Cesar

    ReplyDelete
  5. Vassilis,

    Pls take a look at this link

    http://weblogs.foxite.com/cesarchalom/archive/2006/02/09/1132.aspx

    ReplyDelete
  6. Siegfried DolleischSeptember 6, 2006 at 6:20 AM

    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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. Versión en Español de este artículo in / Spanish version at http://www.portalfox.com/article.php?sid=2170  

    ReplyDelete
  10. Very Useful for me Thanks a lot.....

    ReplyDelete