Me está costando mucho crear miniaturas y luego convertirlas en una matriz de bytes. Probé tres métodos, y los 3 veces recibí un error.Crea una miniatura y luego conviértela en matriz de bytes
El primero estaba usando Bitmap.GetThumbnailImage, que he utilizado en el pasado y luego guardada directamente en Response.OutputStream
El segundo estaba usando System.Drawing.Graphics con DrawImage(). Todavía no ir.
El tercero fue crear un nuevo mapa de bits, pasar el mapa de bits anterior y establecer el nuevo tamaño. Mismo error.
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
Aquí está el código de mi método. Tal vez alguien verá lo que estoy haciendo mal. En caso de que no esté seguro acerca de GetThumbSize, es simplemente un método que abarca el tamaño de la imagen y el tamaño máximo del pulgar y luego calcula un tamaño real para conservar la relación de aspecto.
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();
}
}
}
}
}
}
Esta línea está lanzando el error:
thumb.Save(outStream, thumb.RawFormat);
¿Alguna idea? ¡Gracias por la ayuda!
Este problema en Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –