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!
questo problema in Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –