4

DrawToBitmap用来将一些标签保存为图像。我想知道如何改变这些图像的分辨率,有没有办法?假设我有一个带有文本的标签,我想将其呈现为图像文件(不发布完整代码):

this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 36F);
//...
this.label1.Size = new System.Drawing.Size(161, 54);
this.label1.Text = "Output";
//...

//save image:
Bitmap image = new Bitmap(161, 54);
this.label1.DrawToBitmap(image, this.label1.ClientRectangle);
image.Save(@"C:\image.jpg");

这工作正常,我会得到这样的东西:

结果图像

分辨率没问题,但是可以增加吗?当我稍微放大这张图片时,我可以将单个像素视为大块:

缩放的结果图像

我知道这很正常,因为它不是矢量图形,这很好。我只是想以某种方式对其进行更改,以便您可以在将单个像素视为大块之前进一步放大。有任何想法吗?

谢谢你。

编辑:如果我只使用黑白图像 - 将图像保存为 png 或 gif 会更好吗?

4

2 回答 2

1

像这样的东西可以完成这项工作,只需增加标签中使用的字体大小:

    Bitmap CreateBitmapImage(string text, Font textFont, SolidBrush textBrush)
    {
        Bitmap bitmap = new Bitmap(1, 1);
        Graphics graphics = Graphics.FromImage(bitmap);
        int intWidth = (int)graphics.MeasureString(text, textFont).Width;
        int intHeight = (int)graphics.MeasureString(text, textFont).Height;
        bitmap = new Bitmap(bitmap, new Size(intWidth, intHeight));
        graphics = Graphics.FromImage(bitmap);
        graphics.Clear(Color.White);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.DrawString(text, textFont, textBrush,0,0);
        graphics.Flush();
        return (bitmap);
    }
于 2012-11-02T10:34:26.227 回答
0

您可以通过增加 System.Drawing.Font 的第二个参数的值来实现这一点。

this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 1000F);
于 2012-11-02T10:47:47.687 回答