Привет, я использую 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();
}