Re: Scale bitmaps using GDI+



Bellow is the code that uses Graphics to obtain a scaled bitmap. But, the
result is only a blank bitmap with right dimension. Could you point out which
part I did wrong over there?

// create GDI+ bitmap from the original
Gdiplus::Bitmap GDIBmp(pOriginalBmpHeader, pOriginalBmpData);

// get the JPEG image encoder
CLSID imgClsid;
int nEncoderRc = GetGDIPlusEncoderClsid(L"image/jpeg", &imgClsid);

// create a graphics object from original bitmap
Gdiplus::Graphics GDIGrp(&GDIBmp);

// perform image scaling
Gdiplus::RectF rect(0, 0, m_dwNewWidth, m_dwNewHeight);
GDIGrp.DrawImage(&GDIBmp, rect);

HDC hGrahicDC = GDIGrp.GetHDC();
CDC MemDcDest;
MemDcDest.CreateCompatibleDC(CDC::FromHandle(hGrahicDC));

CBitmap bmpDest;
bmpDest.CreateCompatibleBitmap(&MemDcDest, m_dwNewWidth, m_dwNewHeight);

// associate the new bitmap with dc
CBitmap* pOldBmp = MemDcDest.SelectObject(&bmpDest);

// copy the scaled bitmap from Graphics' DC
MemDcDest.BitBlt(0, 0, m_dwNewWidth, m_dwNewHeight,
CDC::FromHandle(hGrahicDC), 0, 0, SRCCOPY);

// create a new scaled GDI+ bitmap
Gdiplus::Bitmap* pBmpScaled = Gdiplus::Bitmap::FromHBITMAP((HBITMAP)bmpDest,
NULL);

// save to file
Gdiplus::Status stat = pBmpScaled->Save(szwFilePath, &imgClsid, NULL);

// clean up
MemDCDest.SelectObject(pOldBmp);
GDIGrp.ReleaseHDC(hDC);

Thanks!
Eric


"Michael Phillips, Jr." wrote:

DrawImage is not only for display. The Graphics object represents a drawing
surface. That surface can be an image, metafile, display, printer, etc.


"Eric" <Eric@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:257B08E6-F899-4A46-B470-FE134D432B4A@xxxxxxxxxxxxxxxx
THe purpose of the scaling is not for display. I need to save the scaled
image in file as JPEG. That means I must bind the scaled image to a
Bitmai/Image object to do that.

Thanks!
Eric


"Michael Phillips, Jr." wrote:

Use the Graphics's method DrawImage to scale your image.

"Eric" <Eric@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9443F4FA-B556-42C1-9EF4-24A8D421D73B@xxxxxxxxxxxxxxxx
In one of my applications, I need to perform image scaling, then save
the
scaled image to a JPEG file. I can load the original image into a
bitmap/Image object, but, cannot find a method in these two classes to
do
the
scaling. Since there is no need to display it, I did not use Graphics
class.
Any ideas?
--
Thanks!
Eric






.



Relevant Pages

  • Re: Office 2001 --> office 2004
    ... "placeable header", usually a low-resolution bitmap. ... resolution, ...  the issues in graphics between 2001 and 2004 were never ... McGhie Information Engineering Pty Ltdhttp://jgmcghie.fastmail.com.au/ ...
    (microsoft.public.mac.office.word)
  • Re: Invisible Downloaded Pictures Rx
    ... Bitmap' from the document where it was just pasted, ... The issue with a graphics card acceleration, ... Invisible Downloaded Pictures post from Sept. 20th 5:24 PM ...
    (microsoft.public.word.drawing.graphics)
  • The story: Painting bitmap to screen is slow
    ... I've done a lot graphics programming by now and the thing that bothers me with GDI+ in .NET framework the most is slow drawing of bitmaps to screen. ... In Win32, there were two types of bitmaps, compatible and memory bitmap. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: GPath performance issue
    ... I'm drawing a map from coordinates temporarily stored in double arrays. ... the Bitmap to gain performance. ... Is the graphics memory full? ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Bitmap constructor
    ... I would not keep an module-level instance of your Graphics ... Graphics object Windows uses to actually paint the form. ... thread) to paint your bitmap, instead of having your routine do it directly. ...
    (microsoft.public.dotnet.framework.drawing)

Loading