9

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");
7
  • 1
    Is the initial image white? Add this line g.Clear(Colors.Black); just after this line Graphics g = Graphics.FromImage(myBitmap); and check the output. It should be white text over black background.. And don't forget to dispose the graphics after saving your image call g.Dispose(); Jun 19, 2013 at 13:30
  • Lol, yes it is, i just noticed it myself. Thanks very much though! The text is not centered though as i would have expected any input as to why? :) Jun 19, 2013 at 13:31
  • 1
    If it's not centered, what is it? Can you post the image? Jun 19, 2013 at 13:35
  • 1
    Have you changed the Brush of the second call for DrawString? What you are showing us is only the output of the first DrawString call. Your code is working just fine. Jun 19, 2013 at 13:40
  • 1
    In addition you can't see the second text if your bitmap is too small, change the code to this:g.DrawString("My\nText", new Font("Tahoma", 20), Brushes.White, new RectangleF(0, 0, myBitmap.Width, myBitmap.Height), strFormat); and you should see the second output. Jun 19, 2013 at 13:46

1 Answer 1

7

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.