2009-05-21 33 views
7

我有一段時間創建縮略圖,然後將它們轉換成一個字節數組。我已經嘗試了三種方法,並且全部3次出現錯誤。創建一個縮略圖,然後轉換爲字節數組

第一使用Bitmap.GetThumbnailImage,這是我在過去使用,然後直接保存到Response.OutputStream

第二使用System.Drawing.Graphics用的DrawImage()。仍然沒有去。

第三隻是創建一個新的位圖,傳入舊的位圖,並設置新的大小。同樣的錯誤。

Value cannot be null.
Parameter name: encoder

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: encoder

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:
[ArgumentNullException: Value cannot be null.
Parameter name: encoder]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +615244

這裏是我的方法的代碼。也許有人會看到我做錯了什麼。如果您不確定GetThumbSize的大小,它只是一種方法,它會採用圖像大小和最大拇指大小,然後計算實際大小以保留寬高比。

public static System.Drawing.Image.GetThumbnailImageAbort thumbnailCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 

    public static bool ThumbnailCallback() 
    { 
     return false; 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="image"></param> 
    /// <param name="size"></param> 
    /// <remarks> 
    /// This method will throw a AccessViolationException when the machine OS running the server code is windows 7. 
    /// </remarks> 
    /// <returns></returns> 
    public static byte[] CreateThumbnail(byte[] imageData, Size size) 
    { 
     using (MemoryStream inStream = new MemoryStream()) 
     { 
      inStream.Write(imageData, 0, imageData.Length); 

      using (System.Drawing.Image image = Bitmap.FromStream(inStream)) 
      { 
       Size thumbSize = GetThumbSize(new Size(image.Width, image.Height), size); 

       //do not make image bigger 
       if (thumbSize.Equals(image.Size) || (image.Width < size.Width || image.Height < size.Height)) 
       { 
        //if no shrinking is ocurring, return the original bytes 
        return imageData; 
       } 
       else 
       { 
        using (System.Drawing.Image thumb = image.GetThumbnailImage(thumbSize.Width, thumbSize.Height, thumbnailCallback, IntPtr.Zero)) 
        { 

         using (MemoryStream outStream = new MemoryStream()) 
         { 
          thumb.Save(outStream, thumb.RawFormat); 

          return outStream.ToArray(); 
         } 
        } 
       } 
      } 
     } 

    } 

此行引發錯誤:

thumb.Save(outStream, thumb.RawFormat); 

任何想法?謝謝您的幫助!

+1

Microsoft Connect上的此問題:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 – 2009-09-14 19:11:05

回答

6

我認爲這個問題可能是原始圖像的編碼。 IIRC,保存(流,格式)結果調用保存(流,編碼器,參數),編碼器取自格式;在你的情況下是圖像的原始格式。

根據Save method的社區內容,某些格式不會很好地轉換爲適當的編碼器。

我建議你自己指定編碼器,使用像PNG這樣的標準格式。

嘗試:

thumb.Save(outStream, ImageFormat.Png, null); // optionally add encoder parameters here, like quality or luminescence 
+2

您的建議讓我嘗試了一些東西。我剛剛寫了一個將ImageFormat轉換爲mimetype的方法。我通過這個運行原始圖像的格式,並返回「image/gif」,這是預期的,因爲文件擴展名是.gif。 然後我用如下代替冒犯: thumb.Save(outStream,image.RawFormat); 它的工作原理。謝謝您的幫助。 – Josh 2009-05-21 18:24:50

1

如果你正在嘗試做的是保存在一個Raw格式,你可以嘗試以下方法,在我的情況下,它的工作原理,當原始圖像格式是一個支持的:

try 
{ 
    thumb.Save(outStream, img.RawFormat); 
} 
catch (System.ArgumentNullException) 
{ 
    thumb.Save(outStream, ImageFormat.Png); 
} 
相關問題