使用.DrawToBitmap-如何更改图像的分辨率? [英] Using .DrawToBitmap - how to change resolution of image?
问题描述
我使用DrawToBitmap
将某些标签另存为图像.我想知道如何更改这些图像的分辨率,有没有办法?假设我有一个带有文本的标签,并且想将其呈现为图像文件(而不是完整代码):
Im using DrawToBitmap
to save some labels as image. I would like to know how to change the resolution of these images, is there a way? Say I have a label with a text and I want to render it to an image file (not posting the full code):
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");
这工作正常,我会得到这样的东西:
This is working fine, I will get something like this:
分辨率还可以,但是可以提高分辨率吗?当我稍微放大该图像时,我可以看到单个像素为大块:
The resolution is ok, but is it possible to increase it? When I zoom into this image a little, I can see the individual pixels as large blocks:
我知道这很正常,因为它不是矢量图形,这很好.我只是想以某种方式进行更改,以便您可以进一步放大,然后再将单个像素视为大块.有什么想法吗?
I know that's normal, because its not a vector graphic and that's fine. I just would like to change it somehow, so that you can zoom in further before seeing individual pixels as large blocks. Any ideas?
谢谢.
如果我仅使用黑白图像-将图像另存为png或gif更好?
If Im using only black/white images - is it maybe better to save the image as png or gif?
推荐答案
像这样的事情就可以了,只是增加标签中使用的字体大小:
Something like this will do the job, just increase font size used from label:
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);
}
这篇关于使用.DrawToBitmap-如何更改图像的分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!