Saturday, 16 November 2013

Image Resizer Class Example

Leave a Comment
Images often represent a significant fraction of both the data required by the browser to render a page fully and a site’s bandwidth use. For those reasons, it’s important to make sure that you don’t send large images to the client when smaller ones will work just as well. If your images are too big or have a much higher quality than your users need, you might of course choose to resize or recompress them statically.

An alternative is to resize and recompress your images dynamically and cache the results as you go.
You might create a user control to do that, for example, or for a large library of images, you might do it in a background thread. Since the number and size of the images could be large and since IIS has an
efficient method for caching static files, you should generally store the resized images as files rather than in memory.

Here is a example of class Image Resizer in C#

The ImageResizer Class

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading.Tasks;


namespace BlogExample
{
    public class ImageResizerClass
    {
        private static ImageCodecInfo jpgEncoder;
        /// <summary>
        /// Method to resize JPEG and GIF Image
        /// </summary>
        /// <param name="inFile">
        /// Image path and file name  - file In
        /// </param>
        /// <param name="outFile">
        /// Image path and file name  - file Output
        /// </param>
        /// <param name="maxDimension">
        /// the resized image as the length of the longest dimension.
        /// </param>
        /// <param name="level">
        /// level of compression between 0 and 100, with 0 being maximum compression and minimum size and 
        /// with 100 being minimum compression and maximum size.(For JPEG Only)
        /// </param>

        public static void ResizeImage(string inFile, string outFile,double maxDimension, long level)
        {
            byte[] buffer;
            using (Stream stream = new FileStream(inFile, FileMode.Open))
            {
                buffer = new byte[stream.Length];
                Task<int>.Factory.FromAsync(stream.BeginRead, stream.EndRead, buffer, 0, buffer.Length, null);
            }

            using (MemoryStream memStream = new MemoryStream(buffer))
            {
                using (Image inImage = Image.FromStream(memStream))
                {
                    double width;
                    double height;
                    if (inImage.Height < inImage.Width)
                    {
                        width = maxDimension;
                        height = (maxDimension / (double)inImage.Width) * inImage.Height;
                    }
                    else
                    {
                        height = maxDimension;
                        width = (maxDimension / (double)inImage.Height) * inImage.Width;
                    }

                    using (Bitmap bitmap = new Bitmap((int)width, (int)height))
                    {
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.SmoothingMode = SmoothingMode.HighQuality;
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            graphics.DrawImage(inImage, 0, 0, bitmap.Width, bitmap.Height);
                            if (inImage.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                            {
                                if (jpgEncoder == null)
                                {
                                    ImageCodecInfo[] ici = ImageCodecInfo.GetImageDecoders();
                                    foreach (ImageCodecInfo info in ici)
                                    {
                                        if (info.FormatID == ImageFormat.Jpeg.Guid)
                                        {
                                            jpgEncoder = info;
                                            break;
                                        }
                                    }
                                }
                                if (jpgEncoder != null)
                                {
                                    EncoderParameters ep = new EncoderParameters(1);
                                    ep.Param[0] = new EncoderParameter(Encoder.Quality, level);
                                    bitmap.Save(outFile, jpgEncoder, ep);
                                }
                                else
                                {
                                    bitmap.Save(outFile, inImage.RawFormat);
                                }
                            }

                            else
                            {
                                //
                                // Fill with white for transparent GIFs
                                //
                                graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height);
                                bitmap.Save(outFile, inImage.RawFormat);

                            }
                        }
                    }
                }
            }
        }
    }
}

Example to call method

ImageResizer.ResizeImage(this.Server.MapPath("~/launch.jpg"), this.Server.MapPath("~/launch_th.jpg"), 150.0, 60); 

Reference


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.