从ImageFormat.MemoryBMP确定文件类型

Chr*_*ris 5 c# asp.net image

调整图像大小后,我的调整大小函数返回一个新绘制的图像.我遇到了一个问题,我需要确定返回的文件扩展名Image应该是什么.我以前使用过该Image.RawFormat属性,但每次从这个函数返回一个Image时ImageFormat.MemoryBMP,而不是ImageFormat.Jpeg或者ImageFormat.Gif例如.

所以基本上我的问题是,如何确定新调整大小的文件类型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;
        }
Run Code Online (Sandbox Code Playgroud)

Mig*_*uel 7

调整大小的图像不是基于任何文件的格式,它是图像中像素的未压缩存储器表示.

要将此映像保存回磁盘,需要以所选格式对数据进行编码,您必须指定该格式.查看Save方法,它将ImageFormat作为第二个参数,使Jpeg或任何最适合您的应用程序的格式.