views:

1060

answers:

1

I'm having a heck of a time with creating thumbnails and then converting them into a byte array. I've tried three methods, and all 3 times I get an error.

The first was using Bitmap.GetThumbnailImage, which I have used in the past and then saved directly into Response.OutputStream

The second was using System.Drawing.Graphics with DrawImage(). Still no go.

The third was just to create a new bitmap, pass in the old bitmap, and set the new size. Same 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

Here is the code for my method. Maybe someone will see what I'm doing wrong. In case you aren't sure about GetThumbSize, it's simply a method that takes in the image size and the maximum thumb size and then computes an actual size to preserve the aspect ratio.

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

    }

This line is throwing the error:

thumb.Save(outStream, thumb.RawFormat);

Any ideas? Thanks for the help!

+2  A: 

I think the problem may be the original image's encoding. IIRC, Save(stream, format) results in a call to Save(stream, encoder, params), with the encoder being taken from the format; which in your case is the original format of the image.

According to the Community Content for the Save method, some formats will not translate well into an appropriate encoder.

I would suggest you specify the encoder yourself, using some standard format like PNG.

Try:

thumb.Save(outStream, ImageFormat.Png, null); // optionally add encoder parameters here, like quality or luminescence
Randolpho
Your suggestion lead me to try something. I had just written a method to convert the ImageFormat into a mimetype. I ran the format of the original image through this and it returned "image/gif", which was as expected because the file extension is .gif.Then I replaced the offending like with:thumb.Save(outStream, image.RawFormat);and it works. Thanks for the help.
Josh