[ACCEPTED]-GDIplus Scale Bitmap-gdi+

Accepted answer
Score: 15

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

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

or the scaling 7 factors if the new dimensions are known

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

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

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

Create a new Graphics 4 to draw on the created bitmap:

Graphics g(img);

Apply a scale 3 transform to the Graphics and draw the image 2

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

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

Score: 1

The solution proposed by mhcuervo works well, except if the original 8 image has a specific resolution, for example 7 if it was created by the reading of an image 6 file.

In this case you have to apply the 5 resolution of the original image to the 4 scaling factors:

Image* img = new Bitmap((int) newWidth, (int) newHeight);
horizontalScalingFactor *= originalBitmap->GetHorizontalResolution() / img->GetHorizontalResolution();
verticalScalingFactor *= originalBitmap->GetVerticalResolution() / img->GetVerticalResolution();

(note: the default resolution 3 for a new Bitmap seems to be 96 ppi, at least 2 on my computer)

or more simply you can change 1 the resolution of the new image:

Image* img = new Bitmap((int) newWidth, (int) newHeight);
img->SetResolution(originalBitmap->GetHorizontalResolution(),  originalBitmap->GetVerticalResolution());    
Score: 0


Bitmap myBitmap = new Bitmap("Spiral.png");
Rectangle expansionRectangle = new Rectangle(135, 10,
   myBitmap.Width, myBitmap.Height);

Rectangle compressionRectangle = new Rectangle(300, 10,
   myBitmap.Width / 2, myBitmap.Height / 2);

myGraphics.DrawImage(myBitmap, 10, 10);
myGraphics.DrawImage(myBitmap, expansionRectangle);
myGraphics.DrawImage(myBitmap, compressionRectangle);


More Related questions