以编程方式向图像添加文本显得模糊

Adding text to an Image programmatically appears blurry

我正在尝试以编程方式向图像添加一些文本,稍后我将其保存。但最终的结果有些没有说服力。

图片上的文字看起来很模糊。

我用来渲染的片段如下:

static void ModifyImage()
{
    Image origImage;
    if (File.Exists(path))
    {
        using (Stream s = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            origImage = Image.FromStream(s);
        }

        using (Image newImage = new Bitmap(path))
        {
            // Get the image's original width and height
            int originalWidth = origImage.Width;
            int originalHeight = origImage.Height;

            // To preserve the aspect ratio
            float ratio = Math.Min((float)originalWidth, (float)originalHeight);

            Graphics graphics = Graphics.FromImage(newImage);
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            string strSoftwareVersion = "Version " + StrVersion;

            var oVersionFont = new Font("Arial", 15f, FontStyle.Bold);
            SizeF strSize = graphics.MeasureString(strSoftwareVersion, oVersionFont);

            float mX = (float)((originalWidth * 0.85) - 5.0);
            float mY = (float)((originalHeight * 0.85) - 5.0);
            float mWidth = (float) (strSize.Width + 5.0);
            float mHeight = (float)(strSize.Height + 10.0);

            /*
             * Alternate I tried.
             */
            //GraphicsPath blackfont = new GraphicsPath();
            //SolidBrush brsh = new SolidBrush(Color.White);
            //blackfont.AddString("TEST APP", oVersionFont.FontFamily, (int)FontStyle.Bold, 15, new Point((int)(originalWidth * 0.85), (int)(originalHeight * 0.85)), StringFormat.GenericDefault);
            //graphics.FillPath(brsh, blackfont);

            /*
             *Software Version String 
             */
            graphics.DrawString("Version " + StrVersion, new Font("Arial", 15f, FontStyle.Bold), Brushes.White, (int)(originalWidth * 0.85), (int)(originalHeight * 0.85));

            /*
             * Save processed image 
             */
            newImage.Save(newPath, ImageFormat.Jpeg);
        }
        origImage.Dispose();
    }
}


从图像中可以看出,文本和周围区域的模糊度非常突出。我很确定这是由于 alpha 级别的别名或 TextRendering 问题,但无法指出确切的问题。

我遇到了类似的问题,即在将文本绘制到图像上时出现严重的锯齿。这是我的解决方案,可以产生合理的文本质量。

"text" 是一个 System.Windows.Media.FormattedText 实例。

BitmapFrame originalImageSource = BitmapFrame.Create(new Uri(file.FullName), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var visual = new DrawingVisual();

using(DrawingContext drawingContext = visual.RenderOpen()) {
    drawingContext.DrawImage(originalImageSource, new Rect(0, 0, originalImageSource.PixelWidth, originalImageSource.PixelHeight));
    drawingContext.DrawText(text, topRight);
}

var renderTargetBitmap = new RenderTargetBitmap(originalImageSource.PixelWidth,
originalImageSource.PixelHeight,
originalImageSource.DpiX, originalImageSource.DpiY,
PixelFormats.Pbgra32);

renderTargetBitmap.Render(visual);

BitmapFrame bitmapFrame = BitmapFrame.Create(renderTargetBitmap);
BitmapEncoder encoder = new PngBitmapEncoder();

encoder.Frames.Add(bitmapFrame);
encoder.Save(stream);

我觉得文字不错。

为了让它更清晰,您可以关闭所有抗锯齿功能。

'surrounding areas' 显然显示了一些 jpeg 伪影

好的文字和 jpeg 不相配。

要么将默认值调高 jpeg quality settings(大约 75%),要么选择 png

请注意,90-95% 的质量,在大大提高文本质量的同时也大大增加了大小,选择 png 除了无损之外,还可以为您节省 space..