Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a thumbnail and then convert to byte array

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!

like image 897
Josh Avatar asked May 21 '09 17:05

Josh


1 Answers

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
like image 77
Randolpho Avatar answered Sep 30 '22 09:09

Randolpho