Graphics.FromImage method

Applies to: desktop apps only

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

Syntax

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

Parameters

image [in]

Type: Image*

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

Return value

Type:

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

Minimum supported client

Windows XP, Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Product

GDI+ 1.0

Header

Gdiplusgraphics.h (include Gdiplus.h)

Library

Gdiplus.lib

DLL

Gdiplus.dll

See also

Graphics
Graphics::GetHDC
FromHDC Methods
Graphics::FromHWND
Graphics Constructors
Using Images, Bitmaps, and Metafiles
Changes in the Programming Model

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Community Additions

ADD
Show: