I have been working with improving some images in ASP.NET and compiled this code from various MSDN sources. Generally I have found the issues revolve around the concept of anti-aliasing.

private void Button1_Click(object sender, System.EventArgs e)
{
    Bitmap bmp = null;
    Graphics g = null;

    try
    {
        bmp = new Bitmap(@"c:\inetpub\wwwroot\MyWebTest\MYimage.jpg");
        g = Graphics.FromImage(bmp);
        g.CompositingMode = CompositingMode.SourceCopy;
        g.SmoothingMode = SmoothingMode.HighQuality;  //Specifies high quality, low speed rendering
        g.InterpolationMode = InterpolationMode.HighQualityBicubic; //This mode produces the highest quality transformed images.

        Response.ContentType = "image/jpeg"

        //Create a parameter collection
        EncoderParameters codecParameters = new EncoderParameters(1);
        //Fill the only parameter
        codecParameters.Param[0] = new EncoderParameter(Encoder.Quality,100L);
        //Get the codec info
        ImageCodecInfo codecInfo = FindEncoder(ImageFormat.Jpeg);
        //Save the image
        bmp.Save(Response.OutputStream,codecInfo, codecParameters);

    }
    catch
(Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        if (g != null)
        {
            g.Dispose();
        }
        if (bmp != null)
        {
            bmp.Dispose();
        }
    }
}

private static ImageCodecInfo FindEncoder(ImageFormat fmt)
{
    ImageCodecInfo[] infoArray1 = ImageCodecInfo.GetImageEncoders();
    ImageCodecInfo[] infoArray2 = infoArray1;
    for (int num1 = 0; num1 < infoArray2.Length; num1++)
    {
        ImageCodecInfo info1 = infoArray2[num1];
        if (info1.FormatID.Equals(fmt.Guid))
        {
            return info1;
        }
    }
    return null;
}

 

Technorati tags: ,


Comment Section

Comments are closed.