2010-10-28 56 views
4

你好,我想改变规模GDIplus ::位图和保存在内存中缩放BItmap,我有问题。我尝试了很多不同的示例,并且我的结果是NULL。例如,我尝试改变图像分辨率,使用SetResolution,也尝试从图像 - >图形转换位图,并使用构造函数之一GDIplus ::位图比例尺,但我没有结果。例如我尝试下面的代码:GDIplus比例位图

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

回答

0

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); 
+1

的问题是要获得换算出位现有的。不以不同的比例绘制相同的位图。 – mhcuervo 2014-06-05 15:33:05

6

计算的新高度和宽度(如果你有缩放因子)

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

或缩放因子,如果新尺寸已知

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

创建具有足够空间的新的空位图缩放图像

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

创建一个新的图形上绘制创建位图:

Graphics g(img); 

应用比例转换为图形和绘制图像

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

img现在是另一个带缩放版原始图像的位图。

0

solution proposed by mhcuervo效果很好,除非原始图像具有特定的分辨率,例如,如果它是通过读取图像文件创建的。

在这种情况下,你必须将原始图像的分辨率适用于缩放因子:

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

(注:新Bitmap默认分辨率好像是96 ppi的,至少在我的电脑上)

或者更简单地说,你可以更改新图像的分辨率:

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