Question

I use Graphics.DrawString Method to write a text on image, but the text quality become too low after saving image. this id my code:

Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath("~/k32.jpg"));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.DrawString("string", font, s, new Point(10, 10));
graphicImage.InterpolationMode =   System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphicImage.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        graphicImage.TextContrast = 0;
Response.ContentType = "image/jpeg";
bitMapImage.Save(Server.MapPath("~/k33.jpg"), ImageFormat.Jpeg);

How can I improve the text quality? Thanks

Was it helpful?

Solution

You can try using a lossless image format:

bitMapImage.Save(Server.MapPath("~/k33.png"), ImageFormat.Png);

Or if you really want to stick with jpeg then you can try adjusting the JPEG compression level by referring to this MSDN How to: Set JPEG Compression Level

private void VaryQualityLevel()
{
    // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

    // Create an Encoder object based on the GUID
    // for the Quality parameter category.
    System.Drawing.Imaging.Encoder myEncoder =
        System.Drawing.Imaging.Encoder.Quality;

    // Create an EncoderParameters object.
    // An EncoderParameters object has an array of EncoderParameter
    // objects. In this case, there is only one
    // EncoderParameter object in the array.
    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(Server.MapPath("~/k33.jpg"), jgpEncoder, myEncoderParameters);
}


private ImageCodecInfo GetEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}

OTHER TIPS

Try setting the AntiAlias settings before writing the text on the image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top