2009-05-21 8 views
7

Mi sto divertendo un mondo con la creazione di miniature e quindi la conversione in un array di byte. Ho provato tre metodi e tutte e tre le volte ho ricevuto un errore.Creare una miniatura e quindi convertirla in array di byte

Il primo stava usando Bitmap.GetThumbnailImage, che ho usato in passato e quindi salvati direttamente in Response.OutputStream

Il secondo stava usando System.Drawing.Graphics con DrawImage(). Ancora no.

Il terzo era solo per creare una nuova bitmap, passare la vecchia bitmap e impostare la nuova dimensione. Lo stesso errore.

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

Ecco il codice per il mio metodo. Forse qualcuno vedrà cosa sto sbagliando. Nel caso in cui non si sia sicuri di GetThumbSize, è semplicemente un metodo che acquisisce le dimensioni dell'immagine e le dimensioni massime del pollice e calcola una dimensione effettiva per preservare le proporzioni.

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

    } 

Questa linea sta gettando l'errore:

thumb.Save(outStream, thumb.RawFormat); 

Tutte le idee? Grazie per l'aiuto!

+1

questo problema in Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –

risposta

6

Penso che il problema potrebbe essere la codifica dell'immagine originale. IIRC, Salva (stream, format) genera una chiamata a Save (stream, encoder, params), con l'encoder preso dal formato; che nel tuo caso è il formato originale dell'immagine.

In base al contenuto della community per lo Save method, alcuni formati non verranno convertiti correttamente in un codificatore appropriato.

Suggerisco di specificare l'encoder da soli, utilizzando un formato standard come PNG.

Prova:

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

tuo suggerimento mi ha portato a provare qualcosa. Avevo appena scritto un metodo per convertire l'ImageFormat in un mimetype. Ho eseguito il formato dell'immagine originale attraverso questo e ha restituito "image/gif", che era come previsto perché l'estensione del file è .gif. Quindi ho sostituito l'errore come con: thumb.Save (outStream, image.RawFormat); e funziona. Grazie per l'aiuto. – Josh

1

Se ciò che si sta cercando di fare è salvare in un formato Raw è possibile provare quanto segue, come nel mio caso funziona quando il formato immagine originale è una supportata:

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