0

我使用 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);

如何提高文本质量?谢谢

4

2 回答 2

1

在图像上写入文本之前尝试设置 AntiAlias 设置

于 2012-05-09T07:16:16.553 回答
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;
}
于 2012-05-09T07:21:16.603 回答