Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing text on image

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");
like image 248
loveforfire33 Avatar asked Jun 19 '13 13:06

loveforfire33


People also ask

How do I put text onto a picture?

On the Insert tab, in the Text group, click Text Box, drag to draw a text box anywhere near the picture, and then type your text. To change the font or style of the text, highlight the text, right-click it, and then select the text formatting you want on the shortcut menu.

What app lets you draw on a picture?

You Doodle does it all. Draw on photos, add text, insert shapes, work with layers, blending two photos, placing stamps, stickers, scrapbook with collage and frames plus many more tools are available in You Doodle. You may even create custom stamps and stickers right in the app.

How do I add text to a JPEG image?

Add Captions to Photos on Mobile Devices If you have an Android device, use the Google Photos app to add captions. Open the photo and tap the "Edit" icon at the bottom. On the bottom of the screen, scroll past Suggestions, Crop, Adjust and the other options and select "More." Tap "Markup" and then tap the "Text" icon.

How can I draw on a picture?

This icon looks like a pencil and is located in the bottom left corner of your screen. Tap the smiley face and select a drawing tool. Tap the pen to use the pen, the highlighter, or the text tool, to activate the appropriate tool. Draw on the picture.


1 Answers

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.

like image 65
Mohit S Avatar answered Oct 21 '22 08:10

Mohit S