2007-02-22

HOW TO: Save Images in different formats with GdiPlus-X

That's a very easy task for Gdiplus-X:

important:

All samples below use the Gdiplus-x library. Get the latest stable release from codeplex:

http://www.codeplex.com/wiki/view.aspx?projectname=vfpx&title=gdiplusx

do locfile("system.app")

with _screen.system.drawing

   local mybitmap as xfcbitmap
   && create a bitmap object
   mybitmap = .bitmap.new(getpict())

   && save the bitmap in different formats
   mybitmap.save("c:\mypng.png", .imaging.imageformat.png)
   mybitmap.
save("c:\mybmp.bmp", .imaging.imageformat.bmp)
   mybitmap.
save("c:\mytiff.tif", .imaging.imageformat.tiff)
   mybitmap.
save("c:\myjpeg.jpg", .imaging.imageformat.jpeg)
   mybitmap.
save("c:\mygif.gif", .imaging.imageformat.gif)

endwith

Of course, you can save as jpeg using the quality compression encoder parameter:
The sample below asks for an image and saves it as JPEG with quality 25. Try the values from 0 to 100.


do locfile("system.app")


with _screen.system.drawing

   local mybitmap as xfcbitmap
   local myencoderparameter as xfcencoderparameter
   local myencoderparameters as xfcencoderparameters
   && create a bitmap object based on a bmp file.
   mybitmap = .bitmap.new(getpict("bmp"))

   && create an encoderparameters object.
   && an encoderparameters object has an array of encoderparameter objects
   && in this case, there is only one encoderparameter object in the array.
   myencoderparameters = .imaging.encoderparameters.new(1)

   && save the bitmap as a jpeg file with quality level 25.
   && using an encoder object based on the guid
   && for the quality parameter category.
   myencoderparameter = .imaging.encoderparameter.new(.imaging.encoder.quality, 25)
   myencoderparameters.
param.add = myencoderparameter
   mybitmap.
save("c:\myjpeg_025.jpg", .imaging.imageformat.jpeg, myencoderparameters)

endwith

 

4 comments:

  1. myEncoderParameters = .Imaging.EncoderParameters.New(1); <-- the ";" was really a hard thing for me to find out why it didnt work ( esp. also coding php so ; doesnt look too bad sometimes ;-)

    and yes only happend coz i removed some remarks and closed some gaps so it was

    myEncoderParameters = .Imaging.EncoderParameters.New(1);
    myEncoderParameter = .Imaging.EncoderParameter.New(myEncoder, 100)


    for me ... which really freeaked me out ...
    Hi Thomas,
    Thanks for reporting ! I've already updated the text above. Sorry for the inconvenience.
    Regards
    Cesar

    ReplyDelete
  2. Este articulo esta traducido en www.PortalFox.com


    "Como guardar imágenes en diferentes formatos con GdiPlus-X"

    http://www.portalfox.com/article.php?sid=2389


    ReplyDelete
  3. During execution of the above code I seem to get the Program Error "ADD is a methode, event or Object" at the line


    myEncoderParameters.PARAM.ADD = myEncoderParameter

    ReplyDelete
  4. myEncoderParameters.PARAM.ADD ( myEncoderParameter )  :-)

    ReplyDelete