問題描述
我嘗試將 C# 中的字符串(單個字符)繪制到位圖的精確位置:
I try to draw a string (single character) in C# into a Bitmap at an exact position with:
Bitmap bmp = new Bitmap(64, 64);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("W", font1, new SolidBrush(myColor), new Point(32,32);
在單個字母周圍渲染了太多空白空間,我無法猜測繪制角色的所需"位置以使其位于末尾的正確位置.
There is so much empty space rendered around a single letter, that I can not guess the "needed" position to draw the character to have it at the correct position at the end.
現在我有了字符的像素精確尺寸(查看單獨呈現的位圖中的位).但是如果我不能在一個確切的位置(例如中心或右上角或....)繪制字符,這些信息是無用的.
By now I have the pixel exact dimension of the character (looking at bits in a separately rendered bitmap). But this information is useless, if I cannot draw the character at an exact position (e.g. center or top right corner or ....).
是否有其他方法可以在 C# 中在位圖上繪制文本?或者是否有任何轉換方法可以轉換 DrawString 需要的實際像素位置?
Are there other methods to draw text in C# on a bitmap? Or are there any converting methods to convert the real pixel position in something DrawString needs?
推薦答案
無需查看像素或開始使用您自己的字體..
No need to look at the pixels or start working with your own font..
您可以使用 GraphicsPath
而不是 DrawString
或 TextRenderer
,因為它會讓您知道它的網絡邊界矩形 與 GraphicsPath.GetBounds()
.
You can use a GraphicsPath
instead of DrawString
or TextRenderer
, as it will let you know its net bounds rectangle with GraphicsPath.GetBounds()
.
知道了之后,就可以使用 TranslateTransform
計算如何移動 Graphics
對象:
When you know it, you can calculate how to move the Graphics
object using TranslateTransform
:
private void button1_Click(object sender, EventArgs e)
{
string text = "Y"; // whatever
Bitmap bmp = new Bitmap(64, 64); // whatever
bmp.SetResolution(96, 96); // whatever
float fontSize = 32f; // whatever
using ( Graphics g = Graphics.FromImage(bmp))
using ( GraphicsPath GP = new GraphicsPath())
using ( FontFamily fontF = new FontFamily("Arial"))
{
testPattern(g, bmp.Size); // optional
GP.AddString(text, fontF, 0, fontSize, Point.Empty,
StringFormat.GenericTypographic);
// this is the net bounds without any whitespace:
Rectangle br = Rectangle.Round(GP.GetBounds());
g.DrawRectangle(Pens.Red,br); // just for testing
// now we center:
g.TranslateTransform( (bmp.Width - br.Width ) / 2 - br.X,
(bmp.Height - br.Height )/ 2 - br.Y);
// and fill
g.FillPath(Brushes.Black, GP);
g.ResetTransform();
}
// whatever you want to do..
pictureBox1.Image = bmp;
bmp.Save("D:\__test.png", ImageFormat.Png);
}
一個小測試例程,讓我們更好地看到居中:
A small test routine to let us see the centering better:
void testPattern(Graphics g, Size sz)
{
List<Brush> brushes = new List<Brush>()
{ Brushes.SlateBlue, Brushes.Yellow,
Brushes.DarkGoldenrod, Brushes.Lavender };
int bw2 = sz.Width / 2;
int bh2 = sz.Height / 2;
for (int i = bw2; i > 0; i--)
g.FillRectangle(brushes[i%4],bw2 - i, bh2 - i, i + i, i + i );
}
GetBounds
方法返回一個 RectangleF
;在我的示例中,它是 {X=0.09375, Y=6.0625, Width=21, Height=22.90625}
.請注意,由于四舍五入,事物總是可以減去一.
The GetBounds
method returns a RectangleF
; in my example it is {X=0.09375, Y=6.0625, Width=21, Height=22.90625}
. Do note that due to rounding things can always be off by one..
您可能想也可能不想將 Graphics
設置更改為特殊的 Smoothingmodes
等.
You may or may not want to change the Graphics
setting to special Smoothingmodes
etc..
還應注意,這將自動進行,即通過邊界矩形進行機械居中.這可能與 'optical or視覺居中',這很難編碼,并且在某種程度上是個人品味的問題.但排版既是一種職業,也是一種藝術..
Also it should be noted that this will do automatic ie mechanical centering by the bounds rectangle. This may be quite different from 'optical or visual centering', which is rather hard to code and to some extent a matter of personal taste. But typography is as much an art as a profession..
這篇關于如何在精確的像素位置繪制字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!