2013-06-18 88 views
-1

我正在嘗試在C#中調整圖像(位圖)的大小,而不用拉伸圖像。如何在不拉伸的情況下調整圖像大小?

說圖像是100x100像素。

enter image description here

我期待使其100x110像素,在圖像的底部留下一個白色的差距在哪裏它添加了額外的像素。

enter image description here

我已經做到了這一點,但無法找到一個方法來指定像素格式。我需要它是8bppindexed。我附上了一個示例來顯示前後圖像。

這是我到目前爲止的代碼。

string visit2 = "C:\\users\\moorez\\desktop\\visit2.bmp"; 

Bitmap orig = new Bitmap(visit2); 
int width = orig.Width; 
int height = orig.Height; 
int newHeight = height + 2; 
Bitmap newImage = orig.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed); 
newImage.Save("C:\\users\\moorez\\desktop\\visit3.bmp"); 

Bitmap test = new Bitmap(width, newHeight); 
Graphics g = Graphics.FromImage(test); 
g.DrawImage(newImage, new Point(0, 0)); 
test.Save("C:\\users\\moorez\\desktop\\visit4.bmp"); 
+0

請出示你的代碼。您不清楚用什麼方法創建位圖,以及如何在其上繪製另一個位圖來幫助您。 –

+0

GDI +不支持繪製成8bpp圖像。這是非常不平凡的,將色彩深度從1600萬色減少到256色可能會留下很多令人不快的文物。你需要另一個圖形庫,我認爲AForge可以做到這一點。最好的事情是不要打擾,二十年前8bpp重要時640 KB的內存就足夠了。 –

+0

@HansPassant我不想使用這種格式,但有另一個程序將在需要它的圖像上運行。不過謝謝,我會考慮AForge。 – user2009352

回答

0

你可以試試這個

Bitmap bmp = new Bitmap(newImage.Width, newHeight); 
Graphics g = Graphics.FromImage(bmp); 
g.Clear(Color.White); 
g.DrawImageUnscaled(newImage, 0, 0, newImage.Width, newHeight); 
bmp.Save(@"C:\\users\\moorez\\desktop\\visit3.bmp", ImageFormat.Jpeg); 
+0

「我需要它被8bppindexed」 – Matthew

相關問題