3
調整圖像大小後,我的調整大小函數返回一個新繪製的圖像。我遇到了一個問題,我需要確定返回的Image
的文件擴展名應該是什麼。我以前使用的是Image.RawFormat
屬性,但每次從此函數返回圖像時,它都有ImageFormat.MemoryBMP
,而不是ImageFormat.Jpeg
或ImageFormat.Gif
。從ImageFormat.MemoryBMP確定文件類型
所以基本上我的問題是,我如何確定新調整大小的Image
應該是什麼文件類型?
public static Image ResizeImage(Image imageToResize, int width, int height)
{
// Create a new empty image
Image resizedImage = new Bitmap(width, height);
// Create a new graphic from image
Graphics graphic = Graphics.FromImage(resizedImage);
// Set graphics modes
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Copy each property from old iamge to new image
foreach (var prop in imageToResize.PropertyItems)
{
resizedImage.SetPropertyItem(prop);
}
// Draw the new Image at the resized size
graphic.DrawImage(imageToResize, new Rectangle(0, 0, width, height));
// Return the new image
return resizedImage;
}
感謝您澄清我對此的理解。 – Chris