How to draw moving text with GDI+ - c#

The task is to draw a moving text on a black background (for example, a moving number) with .NET GDI+. Some other elements may present on this background, so it isn't possible to fill all the area with black and then draw a string in a new position.
My current code is as follows:
Graphics g = this.CreateGraphics();
// drawing a string
Font myFont = new Font("Fixedsys", 10);
g.DrawString("1", myFont, Brushes.Gray, 100, 100);
// erasing a string
g.DrawString("1", myFont, Brushes.Black, 100, 100);
// then we repeat the code above with a new position for a string
The problem is that the text isn't erased entirely by the second DrawString with black brush. Small border remains visible. Please help, how to remove this trace, and draw moving text correctly.

The problem is solved by adding the following:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;

Related

Graphics.DrawString renders text with black outline on a transparent background

I'm drawing a string on a Bitmap with transparent background using Graphics.DrawString() and I get text with a black contour, when the Font size is smaller than 23 millimeters (the Font is created with GraphicsUnit.Millimeter).
Code:
Bitmap bmp = new Bitmap(2000, 2000);
Color alpha = Color.FromArgb(0, 0, 0, 0);
for (int x = 0; x < bmp.Width; x++)
for (int y = 0; y < bmp.Height; y++)
bmp.SetPixel(x, y, alpha);
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
Font labelFont = new Font("Cascadia Mono SemiBold", 23/*22*/, FontStyle.Regular, GraphicsUnit.Millimeter);
Brush brush = new SolidBrush(Color.White);
g.DrawString("Some text", labelFont, brush, 200, 200);
23 Millimeters-Units font:
22 Millimeters-Units font:
I tried to use TextRenderer, but this draws text without transparent background.
The code presented here has multiple problems:
The initial loop is counter-productive for multiple reasons:
Tries to fill a Bitmap with a transparent color, but this is already the non-color associated with a newly created Bitmap (Color.FromArb(0, 0, 0, 0))
Uses the SetPixel() method, the slowest possible tool for the task
If needed, a Bitmap can be filled with a Color using the Graphics.Clear() method, which calls a native GDI+ function to perform the task
Setting an InterpolationMode in this context is not useful, this property selects the algorithm used to scale or rotate images
The SmoothingMode property selects the algorithm used to anti-aliasing lines, curves and the edges of filled areas. It doesn't apply to the rendering of Fonts, so has no effect on the drawn text. It applies to text rendered with a GraphicsPath, since the text is converted to curves
None of the disposable objects (Graphics, Font, Brush) is either disposed explicitly or declared with using statements (which is pretty bad). It's not clear when the Bitmap is disposed, but could be the duty of the code that uses it
To specify the rendering mode of Fonts, the TextRenderingHint property is used instead. Since it's not specified, the System default smoothing of Font is used, usually ClearType.
About this rendering form, see the notes in:
Drawing a Long String on to a Bitmap results in Drawing Issues
ClearType uses intra-pixel smoothing, designed initially for LCD screens, to blend text with a background; it's especially effective with small Fonts sizes. It doesn't support alpha colors (not in this context, at least).
The device context in which the text is rendered, a GDI+ MemoryBitmap, doesn't use or understand this type of hinting (smoothing), so the pixels that fail to render are filled with an empty color, which notoriously appears as black
A black-ish contour might manifests with different Font sizes (not just the measures reported in the question), when the ClearType hinting fill is less than one pixel
To fix the rendering, remove the clutter, specify a suitable TextRenderingHint mode and declare correctly all disposable objects.
I'm not including the Bitmap, because I don't know how it's used. It must be disposed at some point, of course (very important, it allocates unmanaged resources, the Garbage Collector cannot help you)
var bmp = new Bitmap(2000, 2000);
using (var g = Graphics.FromImage(bmp))
using (var font = new Font("Cascadia Mono SemiBold", 22, FontStyle.Regular, GraphicsUnit.Millimeter))
using (var brush = new SolidBrush(Color.White)) {
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.DrawString("Some text", font, brush, 200, 200);
}
TextRenderingHint.AntiAliasGridFit is appropriate here (see also the linked notes), the characters are drawn using their anti-aliased glyph bitmap and hinting (smoothing)
TextRenderingHint.AntiAlias can also be used in this context (and costs slightly less)
CompositingQuality.HighQuality is not actually used here, there isn't really any composition, since no image is rendered against a background (which may require gamma correction), but also has no cost. You can keep it, in case you decide to draw a bitmap onto the current, at some point; or simply remove it.
About TextRenderer draws text without transparent background
This is not correct. TextRenderer (GDI) can of course render text with a transparent background, it just doesn't support an alpha color (in this context)
As mentioned, we're working with an in-memory GDI+ Device Context
But, if you draw the same text in a different Device Context, e.g., the surface of a Control, then things change.
Also note that TextRenderer cannot be used to render text when printing (for the same reasons previously described).
Test this code, subscribing to the Paint event of a PictureBox, also adding a background Image (without a background image the result doesn't change, it's just more visible)
Graphics.DrawString() is used to render text with a semi-transparent (ARGB) Color
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.Top;
private void someControl_Paint(object sender, PaintEventArgs e)
{
using (var font = new Font("Segoe UI", 22, FontStyle.Regular, GraphicsUnit.Millimeter))
using (var brush = new SolidBrush(Color.White)) {
TextRenderer.DrawText(e.Graphics, "Some text", font,
new Rectangle(new Point(0, 10), pictureBox1.ClientSize), Color.White, Color.Transparent, flags);
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString("Some text", font, brush, 0, 130);
}
using (Font font1 = new Font("Segoe UI", 8.0f, FontStyle.Regular, GraphicsUnit.Millimeter))
using (Font font2 = new Font("Segoe UI", 5.5f, FontStyle.Regular, GraphicsUnit.Millimeter))
using (var brush = new SolidBrush(Color.FromArgb(100, Color.Black))) {
e.Graphics.DrawString("← TextRenderer", font1, brush, 610, 70);
e.Graphics.DrawString("ForeColor: White, BackColor: Transparent", font2, brush, 610, 110);
e.Graphics.DrawString("← GDI+ Graphics", font1, brush, 610, 190);
e.Graphics.DrawString("ForeColor: White, Hinting: AntiAliasGridFit", font2, brush, 610, 230);
}
}
Resulting in (enlarge it):

How to add different text color to bitmap image?

How to add different text with different colour in bitmap image using wpf.i have written the code it will take only one colour in text line but i want different colour in bitmap
SolidBrush brush = new SolidBrush(System.Drawing.Color.White);
System.Drawing.Brush brush1 = new SolidBrush(System.Drawing.Color.Blue);
// draw your rectangle below the original image
System.Drawing.Font font = new System.Drawing.Font("Arial", fontsize, System.Drawing.FontStyle.Bold, GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
graphics.DrawString(multiLineString, font, brush1, position);
please help me out from this problem
Based on your comment - you just need following:
create all the brushes you need - having red, blue and green colors;
split your source string into "i am", "going" and "home" strings;
calculate width in pixels of each string using font you've created and Graphics.MeasureString method;
draw first string from your source position using red brush;
increment position X coordinate with first string width in pixels
draw the rest of your strings using brushes you want in the same manner.

Fill A Graphic With Text Using RectangleF and DrawString

I've been asked to generate placeholder graphics for some items in a database but with one caveat - the font size needs to be dynamic so that the text fills the entire graphic.
The first iteration of the code works just fine, but the client wants to the font to be bigger so that the graphics are easier to read. Here's a snippet of the part which generates the text:
using (Graphics Graphic = Graphics.FromImage(Img))
{
// Add Some Padding
Width = Width - 20;
Height = Height - 20;
// Generate The Text
Font GraphicFont = new Font("Arial", 26, FontStyle.Bold);
RectangleF RectF = new RectangleF(10, 10, (float)Width, (float)Height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
Graphic.DrawString(Title, GraphicFont, Brushes.Black, RectF, sf);
}
Is there I can detect text overflow in my RectangleF, or can I detect the size of my DrawString before I apply it to the graphic, or is there some other magic I can do to get the text to fill the graphic?
I would like the text to wordwrap and the font to be the same size for all the words, so it doesn't necessarily need to fill the graphic entirely - hope this all makes sense.
Use Graphics.MeasureString (documentation: http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx)
If you want to size text to fill a region, you can do binary search on different sizes to determine the largest size that still fits in your target region, then use that size to actually draw the string.

Filling a specific region of a shape containing another shape

In Winforms application, i am doing 2D drawing.
for example, when i try fill a rectangle, that contains a circle. i want only region outside of circle should be filled with the specified color.
i tried but entire rectangle is getting filled.
Just try this to get the desired output. Open a windows forms and add a button. In the button click event, just add this code:
Region rgn = new Region(new Rectangle(50, 50, 200, 150));
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(60, 60, 100, 100);
rgn.Exclude(path);
Graphics g = this.CreateGraphics();
g.FillRegion(Brushes.Blue, rgn);
"rgn.Exclude(Path)" will help you to paint the rectangle excluding the circle inside it.

Using DrawString to draw text with no border

I have this code;
public static Image AddText(this Image image, ImageText text)
{
Graphics surface = Graphics.FromImage(image);
Font font = new Font("Tahoma", 10);
System.Drawing.SolidBrush brush = new SolidBrush(Color.Red);
surface.DrawString(text.Text, font, brush, new PointF { X = 30, Y = 10 });
surface.Dispose();
return image;
}
However, when the text is drawn onto my image it's red with a black border or shadowing.
How can I write text to an image without any border or shadow?
EDIT
I solved this by adding;
surface.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
If someone can explain why I needed this that would be great.
When colored text was drawn on a newly created Bitmap object with its default transparent background, it had incomplete black border around it. Solved the problem by using graphics.Clear(Color.White) after initialization.
What you have there appears to be correct. See http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-how-to-draw-text-on-an-image
You could try TextRenderer.DrawText (though note that it's in the System.Windows.Forms namespace so this could possibly be inappropriate):
TextRenderer.DrawText(args.Graphics, text, drawFont, ClientRectangle, foreColor, backColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter);
For the backColor try using Color.Transparent.
Also when working with your graphics, brushes, fonts, etc. be sure to wrap them in using statements so as to only keep them around as long as required...
e.g.
using (var surface = Graphics.FromImage(image))
{
var font = ... // Build font
using (var foreBrush = new SolidBrush(Color.Red))
{
... // Do stuff with brush
}
}
I think there should not any border.
Are it is border or shadow?
Just try with some another font and Font size + FontStyle.Regular and see is there any difference

Categories