I have never worked with drawing before and im having a little issue. I cant seem to get the output of this code to work.
The file is saving but it is not drawing on the text. Can anyone see what i may have done wrong?
EDIT: A silly mistake - the backgrond of the image was white (and the brush colour was!). The text is not centered however as i would have expected. Any ideas why SO? :)
EDIT: Image is below.
Thanks
Bitmap myBitmap = new Bitmap(#"C:\Users\Scott\desktop\blank.bmp");
Graphics g = Graphics.FromImage(myBitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("My\nText",
new Font("Tahoma", 20),
Brushes.White,
new PointF(0, 0));
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText",
new Font("Tahoma", 20), Brushes.White,
new RectangleF(0, 0, 500, 500),
strFormat);
myBitmap.Save(#"C:\Users\Scott\desktop\blank1.bmp");
I am sure you might be looking for this.
rectf = new RectangleF(655, 460, 535, 90); //rectf for My Text
using(Graphics g = Graphics.FromImage(myBitmap))
{
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText", new System.Drawing.Font("Tahoma", 32, FontStyle.Bold), Brushes.Black, rectf, sf);
}
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90); Line is used to show where your text will be written. So before you actually make your make your text You can see where this rectanlge will be created on the image. If you want the center of the image you can find the height and width and divide that by 2 to find the center of the image and than can plot the rectangle parameters accordingly.
Related
I drawing a rectangle with a dynamic number as a string on it and use it for a windows taskbar icon (overlay icon):
private void DrawOverlayIcon(string queue_count) {
Bitmap bitmap = new Bitmap(30, 30, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bitmap);
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, 30, 30);
Rectangle r = new Rectangle(0, 0, 30, 30);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center; // horizontal
sf.LineAlignment = StringAlignment.Center; // vertikal
Font f = new Font("Arial", 14, System.Drawing.FontStyle.Bold);
g.DrawString(queue_count, f, System.Drawing.Brushes.Black, r, sf);
IntPtr hBitmap = bitmap.GetHbitmap();
ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
_icon_overlay = wpfBitmap;
So far so good. But when a user set up the windows zoom function to maybe 120% or 150% the number becomes so large that it is only displayed as one digit.
Can anyone help?
Thanks a lot.
I am using the graphics.MeasureString method to measure the text size. And based on the measured text size, I was the draw the string using the graphics.MeasureString. But in that I was using the StringFormat to measure and draw the string. But I found the text clipping problem in some text like "left".
Please find the code snippet below,
string text = "Left";
Font font = new System.Drawing.Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height), format);
Please find the text clipping while drawing in below screen shot,
Can you please suggest how to solve this issue?
Try the following. I have removed the StringFormat from the code. It works.
string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
//StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height));
Edit1
I have modified the answer as per OP's request to use StringFormat enum.
string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width + 5, size.Height), format);
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.
I'm drawing a string with the folowing code:
public Image DrawString(String lString)
{
Image lImage = new Bitmap(128, 128);
Rectangle rec = new Rectangle(0, 0, lImage.Width, lImage.Height);
Graphics g = Graphics.FromImage(lImage);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 20, FontStyle.Regular);
font = FindBestFitFont(g, lString, font, rec.Size);
g.DrawString(lString, font, Brushes.Red, rec, drawFormat);
return lImage;
}
The font looks very ugly even when i use:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
Is there a way to make the font more smooth?
Try
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
instead.
Have you tried AntiAliasing or ClearType?
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
I would like to draw some text in a rectangle and have it scale to the maximum size that fits within the rectangle.
So far I have this:
Bitmap bitmapImage = new Bitmap(500, 500);
Graphics graphicImage = Graphics.FromImage(bitmapImage);
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var rect = new Rectangle(0, 0, 500, 500);
graphicImage.DrawString( "testing testing 123!", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, rect);
bitmapImage.Save("test.png");
it draws the text but doesn't scale up the font size.
Call Graphics.MeasureString in a binary search loop.