2009-05-21 16 views
7

サムネイルを作成してからバイト配列に変換するのに時間があります。私は3つの方法を試みましたが、3回すべてエラーが発生します。サムネイルを作成してからバイト配列に変換する

最初は、私が過去に使用したBitmap.GetThumbnailImage、使用し、次いで第二の)のdrawImage(とSystem.Drawing.Graphicsを用いたResponse.OutputStream

に直接保存しました。まだ行きません。

新しいビットマップを作成し、古いビットマップを渡して新しいサイズを設定するだけでした。同じエラー。

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についてわからない場合は、画像サイズと最大サムサイズを取り込んで、アスペクト比を維持するために実際のサイズを計算する単純な方法です。

thumb.Save(outStream, thumb.RawFormat); 

任意のアイデア:

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(); 
         } 
        } 
       } 
      } 
     } 

    } 

この行はエラーを投げていますか?助けてくれてありがとう!

+1

のMicrosoft Connectのこの問題:http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –

答えて

6

問題は元の画像のエンコードである可能性があります。 IIRCのSave(ストリーム、フォーマット)は、エンコーダをフォーマットから取得した状態でSave(ストリーム、エンコーダ、params)の呼び出しを行います。あなたの場合はイメージの元の形式です。

コミュニティコンテンツのSave methodによると、一部の形式は適切なエンコーダに正しく変換されません。

PNGのような標準形式を使用してエンコーダを指定することをお勧めします。

試してみてください。

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

あなたの提案が何かをしようとする私をリードしています。 ImageFormatをmimetypeに変換するメソッドを書いたところです。私はこれを使って元のイメージのフォーマットを実行し、ファイル拡張子が.gifなので "image/gif"を返しました。 それから、次のような違反を置き換えました。 thumb.Save(outStream、image.RawFormat); と動作します。助けてくれてありがとう。 – Josh

1

何をやろうとしていることは、元の画像フォーマットがサポートさ1であるとき、私の場合はそれが動作するように、あなたは次のことを試すことができます生の形式で保存されている場合:

try 
{ 
    thumb.Save(outStream, img.RawFormat); 
} 
catch (System.ArgumentNullException) 
{ 
    thumb.Save(outStream, ImageFormat.Png); 
} 
関連する問題