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 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
.
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())
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
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
- How to create high quality (resolution) pie chart using ASP.NET and C#
- What is ASP.NET and what is ASP.NET hosting?
- How to use iTextSharp .NET PDF library to insert text and image in an existing PDF form template