在c#上绘制字符串后提高图像质量,在c#中

时间:2012-05-09 07:12:15

标签: image text bitmapimage drawstring

我使用Graphics.DrawString方法在图像上写入文字, 但保存图像后文字质量会变得太低。 这是我的代码:

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);

如何改善文字质量? 感谢

2 个答案:

答案 0 :(得分:1)

尝试在在图像上书写文字之前设置AntiAlias设置

答案 1 :(得分:0)

您可以尝试使用无损图像格式:

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

或者,如果您真的想坚持使用jpeg,那么可以参考这个MSDN How to: Set JPEG Compression Level来尝试调整JPEG压缩级别

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;
}