2011-04-04 38 views
1

您好我使用C#和Asp.Net4。在C#中使用圖像。調整一個GIF並保存爲JPEG

我創建了一個重新調整源圖像大小的類。 代碼工作的偉大與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(); 
    } 
+0

哪行給出了錯誤?由於GIF圖像使用索引來指定每個像素的顏色,而不是顯式的RGB值,所以您需要使用不同的方法來讀取它。 – ChrisF 2011-04-04 09:13:28

+0

SD.Graphics newImage = SD.Graphics.FromImage(temp); – GibboK 2011-04-04 09:18:13

+0

上面的行創建錯誤。你能給我一個不同方法的例子嗎?我是否也有與PNG文件相同的問題? – GibboK 2011-04-04 09:19:11

回答

1

只是改變這一行:

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

SD.Bitmap temp = new SD.Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); 
+0

非常感謝作品! – GibboK 2011-04-04 09:39:18

2

,我認爲這就是問題所在:

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

您正在使用你剛纔讀入的圖像的像素格式 - 在這種情況下,GIF,所以這是失敗的。

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