C # write text in bitmap

I have the following problem. I want to make some graphics in the form of C # windows. I want to read a bitmap in my program and then write text on that bitmap. In the end, I want this snapshot to load into pictureBox. And that is my question. How can i do this?

an example of how it should work:

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

Is it possible to do this?

+59
c # drawing
Jun 10 2018-11-11T00:
source share
5 answers
 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; 
+123
Jun 10 2018-11-11T00:
source share

A very old question, but I just needed to create it for the application today and found that the settings shown in the other answers do not lead to a clean image (possibly as new parameters are added to later versions of .Net).

Assuming you need text in the center 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; 

Recommendations

+25
Aug 14 '15 at 14:16
source share

You need to use the Graphics class to write to the bitmap.

In particular, 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; 
+13
Jun 10 2018-11-11T00:
source share
 var bmp = new Bitmap(@"path\picture.bmp"); using( Graphics g = Graphics.FromImage( bmp ) ) { g.DrawString( ... ); } picturebox1.Image = bmp; 
+3
Jun 10 2018-11-11T00:
source share

If you want to wrap your text, you must 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

0
Oct 19 '15 at 5:14
source share



All Articles