Custom draw a shape and text
How to draw combination of rectangle or line with string in a single graphics ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Selva GanapathyPosted Nov 27, 2014, 6:36 AM
Hi Mike,
I not sure that what you asking about, But I suppose you need something like the below code.
Random random = new Random (); // get a random instance
value = random.Next(10000, 99999); // get a random value between any range
var image = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); // Get a bitmap
var font = new Font("TimesNewRoman", 25, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); // Get a font
var graphics = Graphics.FromImage(image); // Get a graphics with the bitmap image
graphics.DrawString(value.ToString (), font, Brushes.Red, new PointF(0, 0)); // Add the value in the graphics
Pen p = new Pen(Brushes.Orange, 2.0f); // get pen width
graphics.DrawLine(p, new PointF(0,this.pictureBox1 .Height), new Point(this.pictureBox1.Width,0)); // draw a diagonal line
graphics.DrawLine(p, new PointF(0,0), new Point(this.pictureBox1.Width , this.pictureBox1.Height)); // draw another diagonal line
p.Dispose(); // dispose the pen to avoid memory leak
graphics.SmoothingMode = SmoothingMode.AntiAlias; // Smoothing the pixel
graphics.TextRenderingHint = TextRenderingHint.AntiAlias; // Smoothing the text rendering because stem width may differ
Regards,
Selva Ganapathy K