Re: Scale bitmaps using GDI+



I would suggest this approach:
1) Load the image you want to scale.
2) Create an emtpy bitmap with the desired scaled width, height and pixel
format. The pixel format should match your loaded image.
Gdiplus::Bitmap *GDIBmp = new Gdiplus::Bitmap( m_dwNewWidth,
m_dwNewHeight, GDIOriginalLoadedBmp->GetPixelFormat());
3) Create a Graphics object from this new empty bitmap. You now have a
drawing surface which you will use to draw and scale the image that you
loaded in step1.
// create a graphics object from original bitmap
Gdiplus::Graphics GDIGrp(GDIBmp);
4) Use DrawImage with your loaded image to draw and scale to the new image.
// perform image scaling
Gdiplus::RectF rect(0, 0, m_dwNewWidth, m_dwNewHeight);
GDIGrp.DrawImage(&GDIOriginalLoadedBmp, rect);
5) Dispose of the Graphics object that contains you new scaled image.
6) Save that image with the .jpeg encoder.
// save to file
Gdiplus::Status stat = GDIBmp->Save(szwFilePath, &imgClsid, NULL);



"Eric" <Eric@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1E17E9DF-99C1-4EF1-BA53-C647215FCFF8@xxxxxxxxxxxxxxxx
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: Scale bitmaps using GDI+
    ... You cannot instantiate a Graphics object from an indexed bitmap ... In order to scale an indexed bitmap, ... Use gdi+'s jpeg encoder to save your scaled bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Different ways for persistent graphics in VB.net
    ... in your button_click event you're creating a new bitmap ... You're creating a new Graphics object, ... drawing. ... times and then store the myImage as a bitmap or jpg. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Graphics.GetHdc/Graphics.FromImage/R2_MASKPEN Woos
    ... graphics object passed to you from a bitmap and successfully create a useful ... GDI DC from it. ... > I do not have a bitmap to draw on, I have a Graphics object. ... > int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Should I store Graphics object ?
    ... I am trying to implement drawing on the bitmap and using bitblt to transfer ... it to the control graphics object in the paint event. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Should I store Graphics object ?
    ... I am trying to implement drawing on the bitmap and using bitblt to transfer ... it to the control graphics object in the paint event. ...
    (microsoft.public.dotnet.framework.windowsforms)