c# write text on bitmap - c#

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?

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;

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
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.pixeloffsetmode(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.drawing.stringformat(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx

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;

var bmp = new Bitmap(#"path\picture.bmp");
using( Graphics g = Graphics.FromImage( bmp ) )
{
g.DrawString( ... );
}
picturebox1.Image = bmp;

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

Related

Write text on image C# with accurate location

The thing is that I’m trying to write txt on image using setting from an editable C# label through my custom edit panel (txt, font name, font size, font style, location), then after I finish the edit I press add the text to the image!
It always gets written in wrong location and wrong font size
The picture is loaded in picture box and the size mode is stretched.
The interface preview
Code:
Bitmap bmp = (Bitmap)pictureBox1.Image;
RectangleF rectf = new RectangleF(NamePreviewPanel.Location.X , NamePreviewPanel.Location.Y, bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
// add the color
int gW = (int)(TestFontLable.Text.Length * TestFontLable.Font.Size);
gW = gW == 0 ? 10 : gW;
LinearGradientBrush LGBrush = new LinearGradientBrush(rectf, TestFontLable.ForeColor, TestFontLable.ForeColor, LinearGradientMode.Vertical);
g.DrawString(TestFontLable.Text, TestFontLable.Font, LGBrush, rectf, format);
pictureBox1.Image = bmp;

Get bitmap and draw it into image

I am trying to save my drawing from picturebox into bitmap and draw that bitmap into image. So far, nothing has appeared in the final image, but while debugging I can only say, that the original bitmap is not null and with/height are correct. However nothing appears after I draw it into image.
I save my drawing into bitmap like this:
GraphicsPath path = RoundedRectangle.Create(x, y, width, height, corners, RoundedRectangle.RectangleCorners.All);
g.FillPath(Brushes.LightGray, path);
g.SetClip(path);
using (Font f = new Font("Tahoma", 9, FontStyle.Bold))
g.DrawString(mtb_hotspotData.Text, f, Brushes.Black, textX, textY);
g.ResetClip();
bitmap = new Bitmap(width, height, g);
Then save it:
hs.bitmap = new Bitmap(bitmap);
And finally use it:
for (int i = 0; i < imageSequence.Count; i++) {
Graphics g = Graphics.FromImage(imageSequence[i]);
//g.CompositingMode = CompositingMode.SourceOver;
//hotspot.bitmap.MakeTransparent();
int x = hotspot.coordinates[i].X;
int y = hotspot.coordinates[i].Y;
g.DrawImage(hotspot.bitmap, new Point(x, y));
}
return imageSequence;
So far I was not able to find any problem in this solution, therefore I have no idea, where the malfunction is.
You seem to misunderstand the relation of a Bitmap and a Graphics object.
A Graphics object does not contain any graphics; it is a tool used to draw into a bitmap of some sort.
The Bitmap constructor you are using (public Bitmap(int width, int height, Graphics g)) does not really connect the Bitmap and the Graphics object. It only uses the dpi resolution from the Graphics.
You don't show how your Graphics is created. If you want to draw into a Bitmap (as opposed to a control's surface) the most direct way is this:
Bitmap bitmap = new Bitmap(width, height);
bitmap.SetResolution(dpiX, dpiY); // optional
using (Graphics G = Graphics.FromImage(bitmap ))
{
// do the drawing..
// insert all your drawing code here!
}
// now the Bitmap can be saved or cloned..
bitmap.Save(..);
hs.bitmap = new Bitmap(bitmap); // one way..
hs.bitmap = bitmap.Clone(); // ..or the other
// and finally disposed of (!!)
bitmap.Dispose();

Overlay bitmap on another bitmap

On a single bitmap I need to display graphs and text values. So what I did is create a bitmap with points and creating a another bitmap with the text and place on the large bitmap.
I tried using the brush to write the text, but I am not able to see the underlying graphics even though trasparency is set.
Instead I thought to set the transparency for the text bitmap, but the bitmap which I have created are 24 bit rgb. So can we set the transparency for the 24 bit map.
Bitmap textBitmap = null;
textBitmap = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
using (Graphics memoryGrahics =
Graphics.FromImage(textBitmap))
{
memoryGrahics.FillRectangle(Brushes.Black, new Rectangle(0, 0, 100, 100));
memoryGrahics.DrawString(result, f, Brushes.White, x, y);
}
//placing the text bitmap on the graphbitmap
using (Graphics g = Graphics.FromImage(GraphBitmap))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
textBitmap.MakeTransparent();
g.DrawImage(textBitmap, 0, 0);
return GraphBitmap;
}
well.. it seems like you are using 2 different Graphical objects... although 1 Graphics objects with 1 bitmap can handle multiple layouts of custom drawings, like so:
int width = 800, height = 600;
var bit = new Bitmap(width, height);
var g = Graphics.FromImage(bit);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var area = new Rectangle(0, 0, width, height);
g.FillRectangle(new LinearGradientBrush(area, Color.PaleGoldenrod, Color.OrangeRed, 45), area);
g.DrawImage(Image.FromFile(#"your image"), new Point(10, 10));
g.DrawString("sample", new System.Drawing.Font("Tahoma", 56), new SolidBrush(Color.Black), new PointF(50, 50));
pictureBox1.Image = bit;
note that g.DrawImage method can be used to load other bitmaps as well

How can I use DrawString clearly?

I need to draw a string with Graphics class with DrawString method. But image has some black pixels, not the exact color. How can I draw it clearly?
public Image Ciz()
{
Color renk = Color.FromArgb(255, 133, 199);
Bitmap result = new Bitmap(KutuBoyutu.Genislik, KutuBoyutu.Yukseklik);
result.SetResolution(100, 100);
Graphics g = Graphics.FromImage(result);
g.SmoothingMode = SmoothingMode.HighQuality;
Brush drawBrush = new SolidBrush(renk);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Rectangle rect1 = new Rectangle(0, 0, result.Width, result.Height);
Font font = new Font("Arial", 15f, FontStyle.Bold, GraphicsUnit.Pixel);
g.DrawString(Yazi, font, drawBrush, rect1, stringFormat);
drawBrush.Dispose();
return result;
}
Drawing text requires a well-defined background so that the anti-aliasing effect can work properly. You don't have any, you forgot to initialize the bitmap. Which left its pixels at the default, black with an alpha of 0.
So the text renderer will try to alias the letter to blend into a black background. You see that, those almost-black pixels now become very visible against a white background. It will only look good if you draw the bitmap on top of a black background. Fix:
using (Graphics g = Graphics.FromImage(result)) {
g.Clear(Color.White);
// etc...
}
If you cannot fix the background color then you need to give up on anti-aliasing. Set the TextRenderingHint to SingleBitPerPixelGridFit. You won't like it much :)
Add
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
to your code.
TextRenderingHint.AntiAliasGridFit works as well.

Using .DrawToBitmap - how to change resolution of image?

Im using DrawToBitmap to save some labels as image. I would like to know how to change the resolution of these images, is there a way? Say I have a label with a text and I want to render it to an image file (not posting the full code):
this.label1 = new System.Windows.Forms.Label();
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 36F);
//...
this.label1.Size = new System.Drawing.Size(161, 54);
this.label1.Text = "Output";
//...
//save image:
Bitmap image = new Bitmap(161, 54);
this.label1.DrawToBitmap(image, this.label1.ClientRectangle);
image.Save(#"C:\image.jpg");
This is working fine, I will get something like this:
The resolution is ok, but is it possible to increase it? When I zoom into this image a little, I can see the individual pixels as large blocks:
I know that's normal, because its not a vector graphic and that's fine. I just would like to change it somehow, so that you can zoom in further before seeing individual pixels as large blocks. Any ideas?
Thank you.
Edit: If Im using only black/white images - is it maybe better to save the image as png or gif?
Something like this will do the job, just increase font size used from label:
Bitmap CreateBitmapImage(string text, Font textFont, SolidBrush textBrush)
{
Bitmap bitmap = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(bitmap);
int intWidth = (int)graphics.MeasureString(text, textFont).Width;
int intHeight = (int)graphics.MeasureString(text, textFont).Height;
bitmap = new Bitmap(bitmap, new Size(intWidth, intHeight));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawString(text, textFont, textBrush,0,0);
graphics.Flush();
return (bitmap);
}
you can achieve this by increasing the value of second parameter of System.Drawing.Font.
this.label1.Font = new System.Drawing.Font("Baskerville Old Face", 1000F);

Categories