2006-06-10

Use encoder parameters to save your images with GDI+

The method savetofile from GpImage class in _GdiPlus.vcx from vfp9 FFC “saves the image object to a disk file, using specified encoder or image format and optional parameters.”

Here are the most important:

 

quality (supported by jpeg)

Specifies the compression level when you save a jpeg image. a quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.

Sample 1: Aaves image with quality of 70%

local lcsource
lcsource = getpict()
local loimage as gpimage of home() + ffc/_gdiplus.vcx
loimage = newobject("gpimage", home() + "ffc/_gdiplus.vcx")
loimage.createfromfile(lcsource)
loimage.savetofile("c:\myimage.jpg","image/jpeg", "quality=70")


 

transformation (supported by jpeg)

Gdi+ provides the following transformations that can be performed on jpeg images without loss of information:

         rotate 90 degrees
         rotate 180 degrees
         rotate 270 degrees
         flip horizontally
         flip vertically
 

Each of those listed above corresponds to a gdiplus constant, that needs to be passed together with the “transformation” encoder.

The transformation will proceed without loss of information if the file used to construct the image object is a jpeg file and the width and height of the image are both multiples of 16. if the width and height of the image are not both multiples of 16, gdi+ will do its best to preserve the image quality when you apply one of the rotation or flipping transformations.

Sample 2: saves image with quality of 50% and rotate 180 degrees

#define encodervaluetransformrotate90       13
#define encodervaluetransformrotate180      14
#define encodervaluetransformrotate270      15
#define encodervaluetransformfliphorizontal 16
#define encodervaluetransformflipvertical   17


local lcsource
lcsource = getpict()
local loimage as gpimage of home() + ffc/_gdiplus.vcx
loimage = newobject("gpimage", home() + "ffc/_gdiplus.vcx")
loimage.createfromfile(lcsource)
loimage.savetofile("c:\myimage.jpg","image/jpeg", ;
              "quality=50, transformation=14")


This makes rotating and flipping a very simple and quick operation!

For TIFF images, these are the commonest parameters:

 

saveflag (supported by tiff)

         See my article at UTMAG from may 2006: “Multiframe images with GDI+
         http://www.universalthread.com/ViewPageArticle.aspx?ID=841
         used for the creation and manipulation of multiframed images.

 

compression (supported by tiff)

         #define encodervaluecompressionlzw    2
         #define encodervaluecompressionccitt3 3
         #define encodervaluecompressionccitt4 4
         #define encodervaluecompressionrle    5
         #define encodervaluecompressionnone   6


 

colordepth (supported by tiff)
         color depth in bytes per pixel.

 

These are defined by gdi+ but not supported by any of the standard encoders: 'compression', 'scanmethod', 'version' and 'rendermethod'.

Source: msdn

See also this blog post: Rotate / flip images with VFP9 and GDI+

5 comments:

  1. Hi Cesar, one question, what if you already have a GDI object like in reports listeners, and you want to save it to disk. For example:


    Instead of this line

    loImage.CreateFromFile(lcSource)

    You already has loImage but with a Handle the way that reportlistener does, and I want to do a:

    loImage.SaveToFile("c:\MyImage.jpg","image/jpeg", "quality=70")


    How can you do that?


    Thaks



    ReplyDelete
  2. Hi Luis,

    Sorry, but I'm not too familiar with this feature !

    Maybe you should look for Cathy Pountney's great articles on MSDN.

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

    ReplyDelete
  4. Thanks!

    It's almost impossible to find any useful info about encoder parameters.

    ReplyDelete