Image.Save(const WCHAR*, const CLSID*, const EncoderParameters*) method

Applies to: desktop apps only

The Image::Save method saves this image to a file.

Syntax

Status Save(
  [in]  const WCHAR *filename,
  [in]  const CLSID *clsidEncoder,
  [in]  const EncoderParameters *encoderParams
);

Parameters

  • filename [in]
    Type: const WCHAR*

    Pointer to a null-terminated string that specifies the path name for the saved image.

  • clsidEncoder [in]
    Type: const CLSID*

    Pointer to a CLSID that specifies the encoder to use to save the image.

  • encoderParams [in]
    Type: const EncoderParameters*

    Optional. Pointer to an EncoderParameters object that holds parameters used by the encoder. The default value is NULL.

Return value

Type:

Type: Status

If the method succeeds, it returns Ok, which is an element of the Status enumeration.

If the method fails, it returns one of the other elements of the Status enumeration.

Remarks

GDI+ does not allow you to save an image to the same file that you used to construct the image. The following code creates an Image object by passing the file name MyImage.jpg to an Image constructor. That same file name is passed to the Image::Save method of the Image object, so the Image::Save method fails.

Image image(L"myImage.jpg"); 

// Do other operations.

// Save the image to the same file name. (This operation will fail.)
image.Save(L"myImage.jpg", ...); 

Examples

The following example creates an Image object from a PNG file and then creates a Graphics object based on that Image object. The code draws the image, alters the image, and draws the image again. Finally, the code saves the altered image to a file.

The code relies on a helper function, GetEncoderClsid, to get the class identifier for the PNG encoder. The GetEncoderClsid function is shown in Retrieving the Class Identifier for an Encoder.

The technique of constructing a Graphics object based on an image works only for certain image formats. For example, you cannot construct a Graphics object based on an image that has a color depth of 4 bits per pixel. For more information about which formats are supported by the Graphics constructor, see Graphics.

VOID Example_SaveFile(HDC hdc)
{
   Graphics graphics(hdc);

   // Create an Image object based on a PNG file.
   Image  image(L"Mosaic.png");

   // Draw the image.
   graphics.DrawImage(&image, 10, 10);

   // Construct a Graphics object based on the image.
   Graphics imageGraphics(&image);

   // Alter the image.
   SolidBrush brush(Color(255, 0, 0, 255));
   imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);

   // Draw the altered image.
   graphics.DrawImage(&image, 200, 10);

   // Save the altered image.
   CLSID pngClsid;
   GetEncoderClsid(L"image/png", &pngClsid);
   image.Save(L"Mosaic2.png", &pngClsid, NULL);
}

Requirements

Minimum supported client

Windows XP, Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Product

GDI+ 1.0

Header

Gdiplusheaders.h (include Gdiplus.h)

Library

Gdiplus.lib

DLL

Gdiplus.dll

See also

Image

EncoderParameter

EncoderParameters

GetImageEncoders

Image::Save Methods

Image::SaveAdd Methods

Using Image Encoders and Decoders

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012