在图像上绘制文本
我以前从来没有画过画,我有一个小问题.我似乎无法让这段代码的输出工作.
I have never worked with drawing before and im having a little issue. I cant seem to get the output of this code to work.
文件正在保存,但未在文本上绘制.谁能看出我做错了什么?
The file is saving but it is not drawing on the text. Can anyone see what i may have done wrong?
一个愚蠢的错误 - 图像的背景是白色的(画笔颜色是!).然而,文本并没有像我预期的那样居中.任何想法为什么如此?:)
A silly mistake - the backgrond of the image was white (and the brush colour was!). The text is not centered however as i would have expected. Any ideas why SO? :)
图片如下.
谢谢
Bitmap myBitmap = new Bitmap(@"C:\Users\Scott\desktop\blank.bmp");
Graphics g = Graphics.FromImage(myBitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("My\nText",
new Font("Tahoma", 20),
Brushes.White,
new PointF(0, 0));
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText",
new Font("Tahoma", 20), Brushes.White,
new RectangleF(0, 0, 500, 500),
strFormat);
myBitmap.Save(@"C:\Users\Scott\desktop\blank1.bmp");
我相信您可能正在寻找这个.
I am sure you might be looking for this.
rectf = new RectangleF(655, 460, 535, 90); //rectf for My Text
using(Graphics g = Graphics.FromImage(myBitmap))
{
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText", new System.Drawing.Font("Tahoma", 32, FontStyle.Bold), Brushes.Black, rectf, sf);
}
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
Line 用于显示您的文本将被写入的位置.因此,在您实际制作文本之前,您可以看到将在图像上创建此矩形的位置.如果您想要图像的中心,您可以找到高度和宽度并将其除以 2 以找到图像的中心,然后可以相应地绘制矩形参数.
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
Line is used to show where your text will be written. So before you actually make your make your text You can see where this rectanlge will be created on the image. If you want the center of the image you can find the height and width and divide that by 2 to find the center of the image and than can plot the rectangle parameters accordingly.