insert text into graphics

Coperion

New member
Joined
Jun 27, 2013
Messages
2
Programming Experience
Beginner
Hello,

I am trying to make a fairly simple program for my brother, I don't have vast C# knowledge and the little I do have is for gaming. However, I created a mockup of what I am looking to do and was hoping to find some guidance on how to accomplish it. The fields on the left correspond to areas on the right where the numbers are right now, they are just there for visual reference at this time, and one set is missing. No biggy, I need to know if it is possible to insert text into the fields on the left and have them show up on the wheel to the right in the corresponding spot? How would I go about it? What am I attempting to do? Seriously, I don't even know what it would be called therefore I can't find much on the google machine. Aside from that I need to add print functionality so that he can print the wheel onto a 4" round label. Doesn't sound terribly difficult, but maybe it is?

Thank you in advance to anyone who takes the time to try and help me.


2w7loxx.png
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
You would need to use GDI+ to draw that text around the wheel. GDI+ drawing is done on the Paint event of the control that you want to draw on, either in the Paint event handler of a control on a form or in the OnPaint method of a custom control. I would suggest you start by doing some general reading on GDI+ and play around with drawing text and shapes.

In your case specifically, you'll need to transform the Graphics object that you use to do the drawing. I would probably use a PictureBox for the wheel and either display an Image for the wheel itself or draw that too. In the Paint event handler, you'll first call TranslateTransform to shift the origin of the world to the centre of the wheel. You can then call RotateTransform in a loop, drawing the desired text after each rotation. That way you'll end up with each bit of text rotated the appropriate number of degrees. Note that you will need to specify a negative X coordinate when drawing the text, because it will be to the left of the centre and the centre is the origin. This might all sound a bit complex but just think of it as having a bit of paper with a pin in the middle and you simply rotate the paper around the pin and draw text horizontally at each stop as the paper rotates.

As for printing, it's done using GDI+ too, so you'll already have the code to print the wheel. You simply move the GDI+ code you have out of the Paint event handler and into a method that takes a Graphics object as an argument. You can then call that method from the Paint event handler of your PictureBox and also the PrintPage event handler of a PrintDocument.

You'll probably want to do more reading, including the MSDN documentation for the relevant classes, but here are some links that may help:

Very Simple Drawing Program
VS 2008 Draw rotated image on Graphics object?
A beginner's guide to printing in .NET

Some of that code is VB but the principles are exactly the same.
 

Coperion

New member
Joined
Jun 27, 2013
Messages
2
Programming Experience
Beginner
Thank you, took me awhile to get back here. I will look into those techniques and figure it out. Very much appreciated.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
Here's a quick and dirty example that draws a clock face.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    DrawClock(e.Graphics);
}

private void DrawClock(Graphics g)
{
    g.TranslateTransform(150, 150);

    for (int i = 1; i <= 12; i++)
    {
        g.RotateTransform(30);
        g.DrawString(i.ToString(), this.Font, Brushes.Black, 0, -100);
    }
}
DrawClock is called only from the Paint event handler of the form but it could be called from the PrintPage event handler of a PrintDocument too.
 
Top Bottom