7

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?

EDIT: 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? :)

EDIT: Image is below.

enter image description here

Thanks

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");
4

1 回答 1

6

我相信你可能正在寻找这个。

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);行用于显示您的文本将被写入的位置。因此,在您实际制作文本之前,您可以看到将在图像上创建此矩形的位置。如果您想要图像的中心,您可以找到高度和宽度并将其除以 2 以找到图像的中心,然后可以相应地绘制矩形参数。

于 2015-09-07T01:28:58.497 回答