0

I've stored my images in SQL server, and imageContent is a byte array (which contains image data stored in DB). I use following code to create a thumbnail from my main image, currently I set explicit width and height for my thumbnail (40x40), but it will damage my image aspect ratio, how can I find aspect ratio of original image and scale down it so that my original aspect ratio is unchanged?

            Stream str = new MemoryStream((Byte[])imageContent);
        Bitmap loBMP = new Bitmap(str);
        Bitmap bmpOut = new Bitmap(40, 40);
        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.FillRectangle(Brushes.White, 0, 0, 40, 40);
        g.DrawImage(loBMP, 0, 0, 40, 40);
        MemoryStream ms = new MemoryStream();
        bmpOut.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        byte[] bmpBytes = ms.GetBuffer();
        bmpOut.Dispose();
        //end new

        Response.ContentType = img_type;
        Response.BinaryWrite(bmpBytes);//imageContent,bmpBytes
Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • 1
    You read the current size, make some calculations, and create the new size. – Aristos Dec 20 '12 at 17:27
  • well I know, but how can I read current size? it is my main question – Ali_dotNet Dec 20 '12 at 17:28
  • 1
    The `loBMP` that hold your image (from your code), have the `loBMP.Width` and `loBMP.Height` that is the size of your image. – Aristos Dec 20 '12 at 17:30
  • thanks it works but my thumbnail image size is too much! it is even bigger than original image, it is in PNG format, I want to have a small-sized image (in terms of bytes on disk), my image width and height are reduced but its on-disk size are bigger – Ali_dotNet Dec 20 '12 at 18:26
  • 1
    see the answer below of koste, and use this calculations to make your final thubnail size. – Aristos Dec 20 '12 at 18:42

2 Answers2

2

Probably this can help you:

public static Bitmap CreateThumbnail(Bitmap source, int thumbWidth, int thumbHeight, bool maintainAspect)
{
        if (source.Width < thumbWidth && source.Height < thumbHeight) return source;

        Bitmap image = null;
        try
        {
            int width = thumbWidth;
            int height = thumbHeight;

            if (maintainAspect)
            {
                if (source.Width > source.Height)
                {
                    width = thumbWidth;
                    height = (int)(source.Height * ((decimal)thumbWidth / source.Width));
                }
                else
                {
                    height = thumbHeight;
                    width = (int)(source.Width * ((decimal)thumbHeight / source.Height));
                }
            }

            image = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(image))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, width, height);
                g.DrawImage(source, 0, 0, width, height);
            }

            return image;
        }
        catch
        {
            image = null;
        }
        finally
        {
            if (image != null)
            {
                image.Dispose();
            }
        }

        return null;
}
Koste
  • 538
  • 6
  • 17
1

Take a look at ImageResizer project: http://imageresizing.net/

It allows you to do do what you're doing and takes care of the aspect ratio for you automatically.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91