Jump to content
  • Advertisement
Sign in to follow this  

GDI+ and resizing of images and ....

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I started to work myself into GDI+ and encountered my first problem that I do not find a solution for. Neither in MSDN nor anywhere on the net is a description how to resize an image. What I try to do is more than simple: I want to load a JPEG, then resize it (or do other stuff with it) and then save it back to the harddrive. Image does not have a resizing function so I assume I have to create a second object. This but cannot be a Image since I cannot specify any size. Therefore I use 'Bitmap' and pass either a copy of the 'Graphics' object of the original Image or its 'PixelFormat'. Accessing the 'Graphics' object of the new 'Image'(Bitmap) object is no good since I only can blit from another 'Image' with the 'DrawImage' member. So I use the GetHBITMAP() function of the 'Bitmap' object that I created and modify the contents of the memory context inside. I even checked that the image data is on the bitmap, yet when I execute the Save() member I just get a file with the correct dimensions but completely black. Does anybody have a complete source code fully functional or can somebody explain why this has to be so damn complicated ? Any help would be very appreciated. Thanks. P.S. I am using no C#, just plain C++.

Share this post

Link to post
Share on other sites
Here it goes:

/* Load the picture */
Gdiplus::Image imgPicture( L"source.jpg" );

/* Extract the PixelFormat of the image */
gdipifoImage = imgPicture.GetPixelFormat();

/* Get the device context of this dialog */
CClientDC clsClientDC(this);

/* Reallocate the global picture holder */
clsGlobPicture.Create( clsClientDC.m_hDC, imgPicture.GetWidth(), imgPicture.GetHeight() );

/* Request the device context of the holder */
HDC hDC = clsGlobPicture.QueryDC();

/* Allocate a GDI+ graphics object and pass the holders device context */
Gdiplus::Graphics gdiGraphic( hDC );

/* Draw the picture into the holder completely in pixel units */
gdiGraphic.DrawImage( &imgPicture, 0, 0, imgPicture.GetWidth(), imgPicture.GetHeight() );

/* Release the context from the graphic object */

/* Release the holders context */

/* Create a GDI+ Bitmap */
Gdiplus::Bitmap gdiBitmap( clsGlobPicture.GetWidth(), clsGlobPicture.GetHeight(), gdipifoImage );

/* Prepare to receive the GDI+ Bitmaps HBITMAP handle */
Gdiplus::Color gdiColor; HBITMAP hbmBitmap;

/* Get the bitmap */
gdiBitmap.GetHBITMAP(gdiColor, &hbmBitmap);

/* Create a ExBitmap */
CExBitmap clsTemp; clsTemp.Attach(clsClientDC.m_hDC, hbmBitmap);

/* Blit in */
clsTemp.BlitIn( &clsGlobPicture, 0,0,0,0, clsGlobPicture.GetWidth(), clsGlobPicture.GetHeight() );

/* Get the JPEG encoder ID */
CLSID jpgClsid; CPrinterApp::GetCodecClsid( L"image/jpeg", jpgClsid );

/* Save it */
gdiBitmap.Save( L"target.jpg", &jpgClsid, NULL );

CExBitmap is a class that encapsulates some Win32 functions to handle Bitmaps properly. The point is that the image data is definitely transferred to hbmBitmap. I tested it. Further, jpgClsid is correct as well. It might be that some encoding parameters have to be passed but I already read them out before with the image to load the jpeg and passed them with no effect.

Any ideas ?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!