82

I have following problem. I want to make some graphics in c# windows form. I want to read bitmap to my program and after it write some text on this bitmap. In the end I want this picture load to pictureBox. And it's my question. How can I do it?

example, how must it work:

Bitmap a = new Bitmap(@"path\picture.bmp");
a.makeTransparent();
// ? a.writeText("some text", positionX, positionY);
pictuteBox1.Image = a;

Is it possible do to?

nirmus
  • 4,913
  • 9
  • 31
  • 50

5 Answers5

159
Bitmap bmp = new Bitmap("filename.bmp");

RectangleF rectf = new RectangleF(70, 90, 90, 50);

Graphics g = Graphics.FromImage(bmp);

g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf);

g.Flush();

image.Image=bmp;
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
  • 6
    g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; //add this line to make text good looking. – rockXrock Apr 23 '15 at 06:47
  • 4
    @rockXrock: Actually you want to set `TextRenderingHint = TextRenderingHint.AntiAliasGridFit`. The single bit per pixel mode is jagged:https://msdn.microsoft.com/en-us/library/a619zh6z(v=vs.110).aspx – iCollect.it Ltd Aug 14 '15 at 14:04
  • 4
    The one thing missing from all these answers is "How do I know if the string will fit?" (or "How can I fit the bitmap to the string perfectly?"). The answer is to use [Graphics.MeasureString](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.measurestring?view=netframework-4.8) – BlueRaja - Danny Pflughoeft Oct 06 '19 at 10:01
  • g.DrawString() does not support Unicode chars, how can I handle this? – ilCosmico Apr 01 '22 at 13:12
43

Very old question, but just had to build this for an app today and found the settings shown in other answers do not result in a clean image (possibly as new options were added in later .Net versions).

Assuming you want the text in the centre of the bitmap, you can do this:

// Load the original image
Bitmap bmp = new Bitmap("filename.bmp");

// Create a rectangle for the entire bitmap
RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);

// Create graphic object that will draw onto the bitmap
Graphics g = Graphics.FromImage(bmp);

// ------------------------------------------
// Ensure the best possible quality rendering
// ------------------------------------------
// The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). 
// One exception is that path gradient brushes do not obey the smoothing mode. 
// Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property.
g.SmoothingMode = SmoothingMode.AntiAlias;

// The interpolation mode determines how intermediate values between two endpoints are calculated.
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

// Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object.
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

// This one is important
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// Create string formatting options (used for alignment)
StringFormat format = new StringFormat()
{
    Alignment = StringAlignment.Center,
    LineAlignment = StringAlignment.Center
};

// Draw the text onto the image
g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf, format);

// Flush all graphics changes to the bitmap
g.Flush();

// Now save or use the bitmap
image.Image = bmp;

References

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • I just couldn't manage to get a successfull result with this(or with any other method) until I add `GraphicsUnit.Pixel` to the Font's construction, what might am I missing? – mkb Mar 16 '18 at 08:49
  • @mkb You would need to show your own code to see what might be wrong. This was working production code in our system. Thanks – iCollect.it Ltd Mar 22 '18 at 21:43
  • I needed to add `var solidBrush = new SolidBrush (Color.White); g.FillRectangle (solidBrush, rectf);` to create a bitmap with background white and text black. Otherwise it was totally black. – rick Apr 14 '21 at 06:48
  • @PauloGuimarães The example assumes drawing onto an existing bitmap image. Sounds like you were starting with no image or a black image. Obviously you need to tweak colours to suit your situation. – iCollect.it Ltd May 09 '21 at 11:50
15

You need to use the Graphics class in order to write on the bitmap.

Specifically, one of the DrawString methods.

Bitmap a = new Bitmap(@"path\picture.bmp");

using(Graphics g = Graphics.FromImage(a))
{
  g.DrawString(....); // requires font, brush etc
}

pictuteBox1.Image = a;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
4
var bmp = new Bitmap(@"path\picture.bmp");
using( Graphics g = Graphics.FromImage( bmp ) )
{
    g.DrawString( ... );
}

picturebox1.Image = bmp;
Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

If you want wrap your text, then you should draw your text in a rectangle:

RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);

See: https://msdn.microsoft.com/en-us/library/baw6k39s(v=vs.110).aspx

Babak
  • 3,716
  • 6
  • 39
  • 56