Je vais avoir un mal fou à la création de vignettes, puis les convertir en un tableau d'octets. J'ai essayé trois méthodes, et toutes les 3 fois j'ai une erreur.Créer une miniature, puis convertir en tableau d'octets
Le premier utilisait Bitmap.GetThumbnailImage, que je l'ai utilisé dans le passé et puis enregistré directement dans Response.OutputStream
La deuxième utilisait System.Drawing.Graphics avec DrawImage(). Toujours pas aller.
Le troisième était juste pour créer une nouvelle image, passez dans l'ancien bitmap, et définissez la nouvelle taille. Même erreur
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
Voici le code de ma méthode. Peut-être que quelqu'un verra ce que je fais mal. Dans le cas où vous n'êtes pas sûr à propos de GetThumbSize, c'est simplement une méthode qui prend en compte la taille de l'image et la taille maximale du pouce, puis calcule une taille réelle pour préserver le rapport d'aspect.
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();
}
}
}
}
}
}
Cette ligne est de lancer l'erreur:
thumb.Save(outStream, thumb.RawFormat);
Toutes les idées? Merci pour l'aide!
Cette question sur Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98452 –