开发者

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 开发者_JS百科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);


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.


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

In this case you have to 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 a new Bitmap seems to be 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());    


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜