How to draw an image with GDI+

In my application Modeler1, I can display pictures. How can you do that operation using Win32 SDK?

It is not difficult. First you must acquire a Graphics object from GDI+.

On my side, the Graphics object is acquired with the View::OnDraw with :

Graphics graphics(pDC->m_hDC);

I store the graphics object in a context class to have a single point of definition.

Then, you have all the API from GDI+ available with the graphics object.

Just declare an Image object and fill a CStringW with the full path of your picture.

Graphics * graphics = ctxt.GetGraphics();

Image image(CStringW(imageFilePath.c_str()));

if( m_type == ElementType::type_shapes_infrastructure )

{

graphics->DrawImage(&image, rect.left, rect.top, image.GetWidth(), image.GetHeight());

}

The DrawImage member function of Graphics just makes the job.

In my application the draw picture is used first by clicking Picture in the upper left side of the ribbon.

Then, you click in the Properties View the Image […] button.

When the image is not selected, there is a rectangle drawed with the primitive DrawRectangle and DrawLine:


if( m_image.length() == 0 )
{
graphics->DrawRectangle(&colorPen, rect.left, rect.top, rect.Width(), rect.Height());
graphics->DrawLine(&colorPen, p1.x, p1.y, p2.x, p2.y);
graphics->DrawLine(&colorPen, p1.x, p2.y, p2.x, p1.y);
}
<pre>

Then you select an image.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: