Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDIplus Scale Bitmap

Hello i'm trying to change scale GDIplus::Bitmap and save in memory scaled BItmap, and i have problem. I try many different sample, and my result is NULL. For Example I try change resolution for image, using SetResolution, also i try convert bitmap from image->graphic and use one of constructors GDIplus::Bitmap scale, but i haven't result. For example i try next code:

Bitmap *bitmap = new Bitmap((int32)width, (int32)height,PixelFormat32bppARGB);
bitmap=bmp.Clone(0,0,W,H,PixelFormat32bppPARGB);
mBitmap=(void *)bitmap->Clone(0.0f,0.0f,width,height,PixelFormat32bppPARGB);
like image 249
Andriy Mytroshyn Avatar asked Oct 28 '10 09:10

Andriy Mytroshyn


1 Answers

Compute the new width and height (if you have the scaling factors)

float newWidth = horizontalScalingFactor * (float) originalBitmap->GetWidth();
float newHeight = verticalScalingFactor * (float) originalBitmap->GetHeight();

or the scaling factors if the new dimensions are known

float horizontalScalingFactor = (float) newWidth / (float) originalBitmap->GetWidth();
float verticalScalingFactor = (float) newHeight / (float) originalBitmap->GetHeight();

Create a new empty bitmap with sufficient space for the scaled image

Image* img = new Bitmap((int) newWidth, (int) newHeight);

Create a new Graphics to draw on the created bitmap:

Graphics g(img);

Apply a scale transform to the Graphics and draw the image

g.ScaleTransform(horizontalScalingFactor, verticalScalingFactor);
g.DrawImage(originalBitmap, 0, 0);

img is now another Bitmap with a scaled version of the original image.

like image 96
mhcuervo Avatar answered Oct 12 '22 04:10

mhcuervo