在C#中处理图像。调整Gif大小并将其另存为JPEG

在C#中处理图像。调整Gif大小并将其另存为JPEG,c#,asp.net,image-manipulation,gdi,C#,Asp.net,Image Manipulation,Gdi,嗨,我使用C#ans Asp.Net4 我创建了一个类来重新调整源图像的大小。 代码对JPEG文件非常有效,但如果我上传GIF,我会收到以下错误: A Graphics object cannot be created from an image that has an indexed pixel format. 这是我的密码。。有什么想法吗 异常行:SD.Graphics newImage=SD.Graphics.FromImage(临时) private static ImageCodec

嗨,我使用C#ans Asp.Net4

我创建了一个类来重新调整源图像的大小。 代码对JPEG文件非常有效,但如果我上传GIF,我会收到以下错误:

A Graphics object cannot be created from an image that has an indexed pixel format.
这是我的密码。。有什么想法吗


异常行:
SD.Graphics newImage=SD.Graphics.FromImage(临时)

private static ImageCodecInfo GetEncoderInfo(字符串mimeType)
{
int j;
ImageCodeInfo[]编码器;
编码器=ImageCodecInfo.GetImageEncoders();
对于(j=0;jSD.Graphics newImage=SD.Graphics.FromImage(临时);
//质量设置输出图像
newImage.SmoothingMode=SmoothingMode.AntiAlias;
newImage.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;
newImage.InterpolationMode=InterpolationMode.HighQualityBicubic;
newImage.PixelOffsetMode=PixelOffsetMode.HighQuality;
//使用新的宽度/高度绘制图像
DrawImage(原始、0、0、newWidth、newHeight);
//使用编解码器保存位图
临时保存(outputFileName、MyImageCodeInfo、myEncoderParameters);
//处理我们的物品。
原始。处置();
temp.Dispose();
Dispose();
}

我认为这就是问题所在:

   SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat);
您使用的是刚刚读入的图像的像素格式—在本例中为GIF格式,因此这是失败的地方

对此,您需要使用非索引像素格式。

只需更改此行:

SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat); 
进入


哪一行给出了错误?由于GIF图像使用索引指定每个像素的颜色,而不是明确的RGB值,因此需要使用不同的方法在.SD.Graphics newImage=SD.Graphics.FromImage(temp)中读取它;上面的行创建错误。你能给我举个不同方法的例子吗?PNG文件也会有同样的问题吗?我已经快速查看了一下,但它应该可以工作。我期待着行
SD.Bitmap temp=new SD.Bitmap(newWidth,newHeight,original.PixelFormat)将失败,因为您正在使用将被索引的图像像素格式。
SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat); 
SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);