2009-05-21 13 views
7

Mam sporo czasu na tworzenie miniatur, a następnie przekształcanie ich w tablicę bajtów. Próbowałem trzy metody i wszystkie 3 razy otrzymuję błąd.Utwórz miniaturę, a następnie przekonwertuj na tablicę bajtów

Pierwszym był przy Bitmap.GetThumbnailImage, które są używane w przeszłości, a następnie zapisany bezpośrednio do Response.OutputStream

Drugim był przy System.Drawing.Graphics z drawImage(). Wciąż nie ma wyjścia.

Trzeci był po to, aby utworzyć nową bitmapę, przekazać starą bitmapę i ustawić nowy rozmiar. Ten sam błąd.

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

Oto kod dla mojej metody. Może ktoś zobaczy, co robię źle. Jeśli nie masz pewności co do GetThumbSize, jest to po prostu metoda, która przyjmuje rozmiar obrazu i maksymalny rozmiar kciuka, a następnie oblicza rzeczywisty rozmiar, aby zachować proporcje.

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

    } 

Linia ta jest rzucanie błąd:

thumb.Save(outStream, thumb.RawFormat); 

pomysłów? Dzięki za pomoc!

+1

Ten problem na Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –

Odpowiedz

6

Myślę, że problemem może być kodowanie oryginału. IIRC, Save (stream, format) powoduje wywołanie Save (stream, koder, params), przy czym enkoder jest pobierany z formatu; który w twoim przypadku jest oryginalnym formatem obrazu.

Zgodnie z Treścią społeczności dla Save method niektóre formaty nie zostaną przetłumaczone poprawnie na odpowiedni koder.

Proponuję, abyś sam określił koder, używając standardowego formatu, takiego jak PNG.

Spróbuj:

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

Twoja sugestia prowadzić mnie, aby spróbować czegoś. Właśnie napisałem metodę konwersji ImageFormat na typ MIME. Uruchomiłem format oryginalnego obrazu i zwróciłem "image/gif", co było zgodne z oczekiwaniami, ponieważ rozszerzenie pliku to .gif. Następnie zastąpiłem obrazkę podobną do: thumb.Save (outStream, image.RawFormat); i to działa. Dzięki za pomoc. – Josh

1

Jeśli to, co staramy się zrobić, to zapisać go w formacie RAW można wypróbować następujące, jak w moim przypadku to działa, gdy oryginalny format obrazu jest obsługiwany jednym:

try 
{ 
    thumb.Save(outStream, img.RawFormat); 
} 
catch (System.ArgumentNullException) 
{ 
    thumb.Save(outStream, ImageFormat.Png); 
} 
Powiązane problemy