我正在使用下面的代碼來調整tif大小。 tif具有透明度設置的alpha通道。我正在嘗試調整這張圖片的尺寸,並尊重透明度,但此刻它會以黑色背景出現。有任何想法嗎?Alpha通道透明度和調整圖像文件大小
public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height)
{
Size NewSize = new Size(Width, Height);
using (Image OriginalImage = Image.FromFile(OriginalImagePath))
{
//Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead.
PixelFormat Format = OriginalImage.PixelFormat;
if (Format.ToString().Contains("Indexed"))
Format = PixelFormat.Format24bppRgb;
using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat))
{
using (Graphics Canvas = Graphics.FromImage(NewImage))
{
Canvas.SmoothingMode = SmoothingMode.AntiAlias;
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize));
NewImage.Save(NewImagePath, OriginalImage.RawFormat);
}
}
}
}
}
1)對於您遇到問題的圖像,OriginalImage.PixelFormat的值是多少? 2)嘗試保存到PNG。這仍然會給你黑色的像素? – 2010-08-16 20:56:07