在 C# 中处理图像。调整 Gif 大小并将其另存为 JPEG
您好,我使用 C# 和 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(temp);
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
protected void ResizeImageWithAspect(string fileName, string outputFileName, int newWidth, int newResolution, string newCodec, int qualityLevel)
{
// Original Image
SD.Image original = SD.Image.FromFile(fileName);
// Find image aspect ratio computed by image height and width.
float aspect = (float)original.Height / (float)original.Width;
// Calculate the new height using the aspect ratio and the desired new width.
int newHeight = (int)(newWidth * aspect);
// Create a bitmap of the correct size. Bitmap it is used for image manipolation.
SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat);
// Setting the Encoder for the image.
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder; // Guid Encoder
EncoderParameter myEncoderParameter; // Pass a value to an Image Encoder
EncoderParameters myEncoderParameters; //Encapsulates an array of EncoderParameter objects
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo(newCodec);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, qualityLevel);
myEncoderParameters.Param[0] = myEncoderParameter;
//Set image resolution (horizontal and vertical)
temp.SetResolution(newResolution, newResolution);
//Get a Graphics object from the bitmap.
SD.Graphics newImage = SD.Graphics.FromImage(temp);
// Quality settings output image
newImage.SmoothingMode = SmoothingMode.AntiAlias;
newImage.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
newImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
newImage.PixelOffsetMode = PixelOffsetMode.HighQuality;
//Draw the image with the new width/height
newImage.DrawImage(original, 0, 0, newWidth, newHeight);
//Save the bitmap with appropirate Codec
temp.Save(outputFileName, myImageCodecInfo, myEncoderParameters);
//Dispose of our objects.
original.Dispose();
temp.Dispose();
newImage.Dispose();
}
Hi I use C# ans Asp.Net4.
I create a class which re size a source image.
Code work great with JPEG File but if I upload a GIF I receive this error:
A Graphics object cannot be created from an image that has an indexed pixel format.
Here my code.. Any Ideas?
Exception line: SD.Graphics newImage = SD.Graphics.FromImage(temp);
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
protected void ResizeImageWithAspect(string fileName, string outputFileName, int newWidth, int newResolution, string newCodec, int qualityLevel)
{
// Original Image
SD.Image original = SD.Image.FromFile(fileName);
// Find image aspect ratio computed by image height and width.
float aspect = (float)original.Height / (float)original.Width;
// Calculate the new height using the aspect ratio and the desired new width.
int newHeight = (int)(newWidth * aspect);
// Create a bitmap of the correct size. Bitmap it is used for image manipolation.
SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, original.PixelFormat);
// Setting the Encoder for the image.
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder; // Guid Encoder
EncoderParameter myEncoderParameter; // Pass a value to an Image Encoder
EncoderParameters myEncoderParameters; //Encapsulates an array of EncoderParameter objects
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo(newCodec);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, qualityLevel);
myEncoderParameters.Param[0] = myEncoderParameter;
//Set image resolution (horizontal and vertical)
temp.SetResolution(newResolution, newResolution);
//Get a Graphics object from the bitmap.
SD.Graphics newImage = SD.Graphics.FromImage(temp);
// Quality settings output image
newImage.SmoothingMode = SmoothingMode.AntiAlias;
newImage.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
newImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
newImage.PixelOffsetMode = PixelOffsetMode.HighQuality;
//Draw the image with the new width/height
newImage.DrawImage(original, 0, 0, newWidth, newHeight);
//Save the bitmap with appropirate Codec
temp.Save(outputFileName, myImageCodecInfo, myEncoderParameters);
//Dispose of our objects.
original.Dispose();
temp.Dispose();
newImage.Dispose();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这就是问题所在:
您正在使用刚刚读入的图像的像素格式 - 在本例中是 GIF,所以这就是失败的原因。
为此,您需要使用非索引像素格式。
I think this is the problem:
You are using the pixel format of the image you have just read in - in this case GIF, so this is what is failing.
You need to use a non-index pixel format for this.
只需将这一行更改
为:
Just change this line:
into