SUBSCRIBE - [ Tech News ] [ Make Money Blogging Tips ] [ Online Marketing Tips ] [ Web Dev News ]
Powered by MaxBlogPress  

How to retain high resolution of a dynamically created image using ASP.NET and C#

March 27, 2009 by MK  
Filed under C# / ASP.NET, web development

pie_chart Today we will discuss about maintaining high resolution of dynamically created image using ASP.NET and C#. If you want to learn how to create an image - You can view this related article for creating an image dynamically using ASP.NET and C# (This article teaches about creating pie chart but can easily be altered to create any image).

This is a two step process -

First step is to define properties while declaring the Graphics class to be used for creating the image. Following properties can be used for attaining that -

objGraphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

objGraphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

objGraphics.CompositingQuality=System.Drawing.Drawing2D.CompositingQuality.HighQuality;

objGraphics.TextRenderingHint=System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

You can follow this link for more information on Graphics class and the properties used above - http://msdn.microsoft.com/en-us/library/system.drawing.graphics(VS.80).aspx

Second step is saving the image. For creating a high quality image we need to play around with the ImageCodecInfo class that we have created. Here we will loop through the MimeType property of encoders array and will select the matching MimeType.

private static ImageCodecInfo GetEncoderInfo(String mimeType)

    {

        int j;

        ImageCodecInfo[] encoders;

        encoders = ImageCodecInfo.GetImageEncoders();

        for (j = 0; j < encoders.Length; ++j)

        {

            if (encoders[j].MimeType == mimeType)

                return encoders[j];

        }

        return null;

    }

Now we will define the response stream and the type of image we want to create.

using (System.Drawing.Image img = LoadImage()) //LoadImage() will return the image that we have created in the steps above.

        {

            ImageCodecInfo myImageCodecInfo;

            Encoder myEncoder;

            EncoderParameter myEncoderParameter;

            EncoderParameters myEncoderParameters;

            myImageCodecInfo = GetEncoderInfo(”image/jpeg”);

            myEncoder = Encoder.Quality;

            myEncoderParameters = new EncoderParameters(1);

            myEncoderParameter = new EncoderParameter(myEncoder, 100L); //You can use 100L to increase or decrease the quality of the image.

            myEncoderParameters.Param[0] = myEncoderParameter;

            Response.ContentType = “image/gif”;

            img.Save(Response.OutputStream, myImageCodecInfo, myEncoderParameters);

        }

As you see its quite easy to create high quality images using the Graphics class in ASP.NET. Almost everything done by using third party components can be created using built in classes with ASP.NET. Happy Coding!!.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

One Response to “How to retain high resolution of a dynamically created image using ASP.NET and C#”
  1. Rajesh says:

    Hi,

    can u plz attach me with the sample code for the above mentioned example.

    thanks
    Rajesh

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!