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: Saves image with quality of 70%

LOCAL lcSource
m.lcSource = GETPICT()
LOCAL loImage AS gpImage OF HOME() + ffc / _GdiPlus.vcx
m.loImage = NEWOBJECT("gpimage", HOME() + "ffc/_gdiplus.vcx")
m.loImage.CreateFromFile(m.lcSource)
m.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
m.lcSource = GETPICT()
LOCAL loImage AS gpImage OF HOME() + ffc / _GdiPlus.vcx
m.loImage = NEWOBJECT("gpimage", HOME() + "ffc/_gdiplus.vcx")
m.loImage.CreateFromFile(m.lcSource)
m.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+Multiframe images with GDI+
         https://www.levelextreme.com/Home/ShowHeader?Activator=23&ID=39285
         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:
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