views:

58

answers:

2

Hi, I use this code to create thumbnails and then store the original and the thumbnail into a DB. It creates tn that are always of a fixed sized and if the original image is wither than it's higher it is cut and then resized to the fixed size.

The code is working however I would really appreciate some help modifying this code to do the following (I have tried it but didn't succeeded):

  • Make high-quality thumbnails

  • cut the height if the image is way taller
    than it's width (If the width is
    200px and height is 1000px what will happen?)

  • Accept png and tiff.

This is the code so far:

    public void imgUpload()
    {
        if (ImgUpload.PostedFile != null)
        {
            System.Drawing.Image image_file = System.Drawing.Image.FromStream(ImgUpload.PostedFile.InputStream);
            string fileName = Server.HtmlEncode(ImgUpload.FileName);
            string extension = System.IO.Path.GetExtension(fileName);

            bool sizeError = false;

            if(image_file.Width < 200)
                sizeError = true;

            if(image_file.Height < 250)
                sizeError = true;

            if ((extension.ToUpper() == ".JPG") && !sizeError)
            {

                //**** Resize image section ****  
                int image_height = image_file.Height;
                int image_width = image_file.Width;
                int original_width = image_width;
                int original_height = image_height;
                int max_height = 250;
                int max_width = 200;

                Rectangle rect;

                if (image_width > image_height)
                {
                    image_width = (image_width * max_height) / image_height;
                    image_height = max_height;
                    rect = new Rectangle(((image_width - max_width) / 2), 0, max_width, max_height);
                }
                else
                {
                    image_height = (image_height * max_width) / image_width;
                    image_width = max_width;
                    rect = new Rectangle(0, ((image_height - max_height) / 2), max_width, max_height);
                }

                Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                Bitmap new_bitmap_file = bitmap_file.Clone(rect, bitmap_file.PixelFormat);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();

                new_bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Position = 0;

                byte[] imageThumbnail = new byte[stream.Length + 1];
                stream.Read(imageThumbnail, 0, imageThumbnail.Length);

                Bitmap Original_bitmap_file = new Bitmap(image_file, original_width, original_height);
                System.IO.MemoryStream Original_stream = new System.IO.MemoryStream();

                Original_bitmap_file.Save(Original_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                Original_stream.Position = 0;

                byte[] imageOriginal = new byte[Original_stream.Length + 1];
                Original_stream.Read(imageOriginal, 0, imageOriginal.Length);
                //**** End resize image section ****  

                saveImage(imageThumbnail, imageOriginal, IDTextBox.Text);
                lblOutput.Visible = false;
            }
            else
            {
                lblOutput.Text = "Please only upload .jpg files and make sure the size is minimum 200x250";
                lblOutput.Visible = true;
            }
        }
        else
        {
            lblOutput.Text = "No file selected";
            lblOutput.Visible = true;
        }
    }
A: 

Here is an example of how to scale and crop an image:

Bitmap b = new Bitmap(200, 1000);
using (var g = Graphics.FromImage(b))
{
    g.DrawLine(Pens.White, 0, 0, b.Width, b.Height);
}
b.Save("b.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Bitmap thumb = new Bitmap(100, 100);
using (var g = Graphics.FromImage(thumb))
{
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.DrawImage(b, new Rectangle(0,0,100,100), new Rectangle(0, 400, 200, 200), GraphicsUnit.Pixel);
}
thumb.Save("thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

I think you can adapt the code to all what's and if's about when the ratio height/width is to high, etc.

Albin Sunnanbo
This didn't really help...
Mikael
A: 

I use the following code when creating thumbnails for http://www.inventerat.se and a few other sites and it works like a charm:

    public static Bitmap WithinMaxSize(Image imgPhoto, int maxWidth, int maxHeight)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        int destWidth;
        int destHeight;

        float sourceAspect = sourceWidth / (sourceHeight * 1.0F);
        float maxAspect = maxWidth / (maxHeight * 1.0F);
        if (sourceAspect > maxAspect)
        {
            // Width is limiting.
            destWidth = maxWidth;
            destHeight = (int)Math.Round(destWidth / sourceAspect);
        }
        else
        {
            // Height is limiting.
            destHeight = maxHeight;
            destWidth = (int)Math.Round(destHeight * sourceAspect);
        }

        Bitmap bmPhoto = new Bitmap(destWidth, destHeight);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.CompositingMode = CompositingMode.SourceCopy;
        grPhoto.CompositingQuality = CompositingQuality.HighQuality;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto, 0, 0, destWidth, destHeight);

        grPhoto.Dispose();
        return bmPhoto;
    }

Just don't forget to dispose the Bitmap returned from this method.

It does not work very well if users are uploading very large images (e.g. > 5000 * 5000 pixels) though. If that is a requirement, I would recommend using ImageMagick or a similar imaging package.

To accept png and tiff, just adjust how You check the file extension so that You accept "png", "tif" and "tiff" as well. Support for tiff is a bit sketchy in .NET though since tiff itself is only a container for MANY different encodings.

Andreas Paulsson