locked
Image thumbnailing RRS feed

  • Question

  • User168312801 posted

    I know I know, another(!!) post on this. But I have done some reading on it and my thumb nail generation works great with a few exceptions, this is what I need help with.

    My code: 

            public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
            {
                using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
                {
                    Size newSize = CalculateDimensions(oldImage.Size, targetSize);
    
                    using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                    {
                        using (Graphics canvas = Graphics.FromImage(newImage))
                        {
                            canvas.SmoothingMode = SmoothingMode.AntiAlias;
                            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
                            MemoryStream m = new MemoryStream();
                            newImage.Save(m, ImageFormat.Jpeg);
                            return m.GetBuffer();
                        }
                    }
    
                }
            }
    
            private static Size CalculateDimensions(Size oldSize, int targetSize)
            {
                Size newSize = new Size();
                if (oldSize.Width > oldSize.Height)
                {
                    newSize.Width = targetSize;
                    newSize.Height = (int)(oldSize.Height * (float)targetSize / (float)oldSize.Width);
                }
                else
                {
                    newSize.Width = (int)(oldSize.Width * (float)targetSize / (float)oldSize.Height);
                    newSize.Height = targetSize;
                }
                return newSize;
            }

     I am passing the targetSize and imageFile byte stream from the front end and streaming out 4 different sizes to be saved into a database for later use. And agian all is working well for large images. But when it comes to protrait and and images smaller than those being thumbnailed it goes astray. I read the post on dontnetslackers.com and changed a little in the CalculateSize method. Heres what happens:

    Case 1: Images smaller than the thumbnailed size (small 100px, medium 250px, large 500px, and xlarge 700px respectivly) expand the width to the target size and the height gets chopped off.
    Samples: Original, large, xLarge

    Case 2: Images that are portrait and the width is smaller than the target size, expand the width to the target size and grow the height to meet the new width respectivly.
    Samples: Original, large, xLarge

    What I'm needing to do:

    Case 1, If the width is smaller than the target size I want to keep the existing width, and height.
    Case 2, Adjust the height to be the new target size wile maintaining the correct aspect ratio.

    Any help would be grand. Also currently I do not have cacheing in place for the UI to use but that will come with time (maybe). Currently the system operates solely from the database with no dependance on the file system and I'm trying to keep it that way hence why I'm storing the thumbs in the database.

    Thanks,
    Tim

    Sunday, August 10, 2008 11:09 AM

All replies

  • User546723956 posted

     thei post may help you

    http://forums.asp.net/t/1299324.aspx

    hope this helps

    Sunday, August 10, 2008 11:27 AM
  • User168312801 posted

    That might help though I am not sure what it's doing. I'm hardly even a Jr programmer so there is a LOT of things that escape me. My code is very straight forward and easy (for me) to understand. The example you sighted is using things I've never seen liek this: float imgResize = imgSize <= 128 ? (float)1.0 : 128 / imgSize;. I have no idea what it's trying to do.

    I do know there is a lot of discussion on this topic, any one else have something they could chime in with?

    Thanks,
    Tim

    Sunday, August 10, 2008 12:39 PM