GDIplus Bitmap

Hi, I'm trying to zoom in on GDIplus :: Bitmap and save a scalable BItmap in memory, and I have a problem. I try a lot of different samples, and my result is NULL. For example, I will try to change the resolution for the image using SetResolution, I will also try to convert the bitmap from the image-> graphics and use one of the GDIplus :: Bitmap scale constructors, but I did not give it. For example, I try the following 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); 
+4
source share
3 answers

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

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

or scaling factors if new sizes are known

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

Create a new blank raster map with enough space for a scaled image

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

Create new graphics for drawing on the created bitmap:

 Graphics g(img); 

Apply scale transformation to the graphic and draw an image

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

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

+7
source

http://msdn.microsoft.com/en-us/library/e06tc8a5.aspx

 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); 
0
source

The solution proposed by mhcuervo works well, unless the original image has a specific resolution, for example, if it was created by reading the image file.

In this case, you should apply the resolution of the original image to the scaling factors:

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

(note: the default resolution for the new Bitmap similar to 96 ppi, at least on my computer)

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

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

All Articles