Click here to Skip to main content
15,888,264 members
Articles / Web Development / HTML

How to Retain High Resolution of a Dynamically Created Image using ASP.NET and C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Apr 2013CPOL1 min read 8.9K   5  
Maintaining high resolution of dynamically created image using ASP.NET and C#

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:

C#
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 click on the following link for more information on Graphics class and the properties used above:

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.

C#
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.

C#
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);
    //You can use 100L to increase or decrease the quality of the image.
    myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    Response.ContentType = "image/gif";
    img.Save(Response.OutputStream, myImageCodecInfo, myEncoderParameters);
}

As you see, it's 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!!.

How to retain high resolution of a dynamically created image using ASP.NET and C# is a post from: CoolWebDeveloper.com

Related Posts

  1. How to create high quality (resolution) pie chart using ASP.NET and C#
  2. What is ASP.NET and what is ASP.NET hosting?
  3. How to use iTextSharp .NET PDF library to insert text and image in an existing PDF form template

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NetCloud Media
Canada Canada
I am a Senior Systems Developer with a leading financial firm in Toronto. I am a technology enthusiast and have worked on several large enterprise level projects.

You can visit my blog here: http://www.coolwebdeveloper.com

Comments and Discussions

 
-- There are no messages in this forum --