Graphics::FromImage method (gdiplusgraphics.h)

The Graphics::FromImage method creates a Graphics object that is associated with a specified Image object.

Syntax

Graphics * FromImage(
  [in] Image *image
);

Parameters

[in] image

Type: Image*

Pointer to an Image object that will be associated with the new Graphics object.

Return value

Type: Graphics*

This method returns a pointer to the new Graphics object.

Remarks

This method fails if the Image object is based on a metafile that was opened for reading. The Image::Image(filename, useEmbeddedColorManagement) and Metafile::Metafile(filename) constructors open a metafile for reading. To open a metafile for recording, use a Metafile constructor that receives a device context handle.

This method also fails if the image has one of the following pixel formats:

  • PixelFormatUndefined
  • PixelFormatDontCare
  • PixelFormat1bppIndexed
  • PixelFormat4bppIndexed
  • PixelFormat8bppIndexed
  • PixelFormat16bppGrayScale
  • PixelFormat16bppARGB1555

Examples

The following example calls the Graphics::FromImage method to create a Graphics object that is associated with an Image object. The call to Graphics::FillEllipse does not paint on the display device; instead, it alters the bitmap of the Image object. The call to Graphics::DrawImage displays the altered bitmap.

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

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

   // Create a Graphics object that is associated with the image.
   Graphics* imageGraphics = Graphics::FromImage(&image);
   
   // Alter the image.
   SolidBrush brush(Color(255, 0, 0, 255));
   imageGraphics->FillEllipse(&brush, 10, 40, 100, 50);

   // Draw the altered image.
   graphics.DrawImage(&image, 30, 20);
   
   delete imageGraphics;
}

Requirements

Requirement Value
Minimum supported client Windows XP, Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header gdiplusgraphics.h (include Gdiplus.h)
Library Gdiplus.lib
DLL Gdiplus.dll

See also

Changes in the Programming Model

FromHDC Methods

Graphics

Graphics Constructors

Graphics::FromHWND

Graphics::GetHDC

Using Images, Bitmaps, and Metafiles