c# - 使用 .DrawToBitmap - 如何更改图像的分辨率?

标签 c# .net image

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

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

Resultant image

分辨率还可以,但有没有可能提高呢?当我稍微放大此图像时,我可以看到单个像素是大块:

Zoomed resultant image

我知道这很正常,因为它不是矢量图形而且没关系。我只是想以某种方式更改它,以便您可以进一步放大,然后再将单个像素视为大块。有什么想法吗?

谢谢。

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

最佳答案

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

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

关于c# - 使用 .DrawToBitmap - 如何更改图像的分辨率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193212/

相关文章:

c# - SectionInformation.ProtectSection 有多安全?

image - Swift - 读取 .jpg 图像的像素颜色返回不正确的值

c# - 获取 'excelcnv.exe' 位置

.net - OpenXML 每行的文件越大越慢?

C# MySQL 中的连接太多

image - 无法让图像在Qt中中心旋转

css - 图像未缩放和居中

IP 地址的 C# ComboBox

c# - 在运行时禁用 HP UFT API 上的检查点

c# - 新线程无法识别已经创建的主线程单例