I need to draw a string over a Infragistics toolbar, and I cannot use a label since it's background will not be actually transparent (see in image).
I've managed to overlay the text as desired using DrawString method, but the problem is that the text does not resemble a label. it's thicker, aliased and for some reason black.
What should i change im my code to replicate a label's looks using the DrawString method (same font, size, forecolor) ?
And the code:
FontFamily fontFamily = new FontFamily("Microsoft Sans Serif");
Font font = new Font(
fontFamily,
17,
FontStyle.Regular,
GraphicsUnit.Pixel);
SolidBrush solidBrush = new SolidBrush(SystemColors.ControlText);
drawParams.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
drawParams.Graphics.DrawString("String Drawn with DrawString method", font, solidBrush, textEditorLoc.X, textEditorLoc.Y + 25);
Try using TextRenderer class:
TextRenderer.DrawString(
drawParams.Graphics,
"String Drawn with TextRenderer (GDI) method",
font,
new Point(textEditorLoc.X, textEditorLoc.Y + 25),
SystemColors.ControlText);
Also leave TextRenderingHint as SystemDefault and use the same font size as for label.
The standard text size of a label is 8,25 points, or approximately 11 pixels. Your size of 17 pixels results in a text size of 13 pt.
Try using this
FontFamily fontFamily = new FontFamily("Microsoft Sans Serif");
Font font = new Font(
fontFamily,
8.25f,
FontStyle.Regular,
GraphicsUnit.Point);
SolidBrush solidBrush = new SolidBrush(SystemColors.ControlText);
e.Graphics.DrawString("String Drawn with DrawString", font, solidBrush, textEditorLoc.X, textEditorLoc.Y + 25);
Related
I'm using C# DrawString to draw text on an image background which is output as a TIFF for printing.
For some fonts at some sizes, the DrawString method is adding a border around the text. Both of these examples are drawn using the same code, just with different font sizes. The first is 10pt and the second is 12pt.
10 pt Open Sans font:
12 pt Open Sans font:
On other fonts, this change happens at larger sizes. Like if I use Times New Roman, the outline occurs up until 18pt and then it goes away.
This is how I'm creating the background image (I know I should use "using", just not doing so yet):
protected Image CreateCTextBackground() {
// TODO Get DPI from template
Bitmap background = new Bitmap(TEXT_RECT_WIDTH, TEXT_RECT_WIDTH);
background.SetResolution(360f, 360f);
Graphics graphics = Graphics.FromImage(background);
graphics.Clear(Color.Transparent);
graphics.Save();
return background;
}
Creating a font:
protected Font CreateFont(TextFeature textFeature) {
FontFamily fontFamily = new FontFamily(textFeature.FontFamily);
Font font = new Font(
fontFamily,
textFeature.FontSize,
GraphicsUnit.Point);
return font;
}
And then I'm drawing on it:
Image textBackground = CreateCTextBackground();
Graphics textGfx = Graphics.FromImage(textBackground);
Brush textBrush = new SolidBrush(fontColor);
log.Debug($"xPos={xPos},yPos={yPos}");
textGfx.TranslateTransform(xPos, yPos);
textGfx.RotateTransform(270);
textGfx.DrawString(textFeature.Text, font, textBrush, textGfx.VisibleClipBounds, stringFormat);
I'm not sure what I'm doing wrong. Any advice is appreciated.
I'm using C# to write a text in a certain format. My problem is that when I edit font size both width and height are changing while I just want to change the font height.
My code:
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman",11, FontStyle.Bold))
//using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman", 11, FontStyle.Bold))
{
SolidBrush transBrush = new SolidBrush(Color.FromArgb(65, 79, 79));
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
graphics.DrawString(firstname, romanfont, transBrush, firstnameLocation, format);
graphics.DrawString(secondname, romanfont, transBrush, secondnameLocation, format);
graphics.DrawString(finalfirstadd, romanfont, transBrush, firstaddresslocation, format);
graphics.DrawString(finalsecondadd, romanfont, transBrush, secondaddresslocation, format);
}
}
You can achieve this effect by setting a transform on the Graphics object.
For example, if you want to make the text twice as tall but still the same width, you can do this:
graphics.scaleTransform(1, 2);
You would put this anywhere above the place where you draw your strings. Note that this change will make everything twice as tall, so you may need to adjust your positions and sizes of your rectangles (such as firstnameLocation; in this case you'd probably want to divide the top and height of the rectangle by 2.)
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.
Because of lack of Graphics object in certain places in my application, I decided to use TextRenderer class. What is quite surprising though is that it adds a lot of margins to measured text. For example:
private void button1_Click(object sender, EventArgs e) {
using (var g = this.CreateGraphics()) {
Font font = new Font("Calibri", 20.0f, GraphicsUnit.Pixel);
Size size = TextRenderer.MeasureText("Ala ma kota", font);
g.DrawRectangle(Pens.Red, new Rectangle(new Point(10, 10), size));
TextRenderer.DrawText(g, "Ala ma kota", font, new Point(10, 10), Color.Black);
}
}
Gives the following result:
Why does it do so? Is there a way to force it to get the real text size? (and of course draw it in the same rectangle it returns)
From MSDN:
For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces, use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter, as shown in the example.
You must also pass the Graphics object for correct results, because:
This overload of MeasureText(String, Font, Size, TextFormatFlags) will ignore a TextFormatFlags value of NoPadding or LeftAndRightPadding. If you are specifying a padding value other than the default, you should use the overload of MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) that takes a IDeviceContext object.
Size size = TextRenderer.MeasureText(g,
"Ala ma kota",
font,
new Size(int.MaxValue, int.MaxValue),
TextFormatFlags.NoPadding);
TextRenderer.DrawText(g, "Ala ma kota", font,
new Point(10, 10),
Color.Black,
TextFormatFlags.NoPadding);
g.DrawRectangle(Pens.Red, new Rectangle(new Point(10, 10), size));
Also have a look at using the Graphics methods directly: GDI+ MeasureString() is incorrectly trimming text
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.