0

I'm just starting to learn the GDI+ system for drawing lines, circles etc. I've created a component (scanner) that inherits a Panel to draw on (not sure if panel or picture box is best).

On the "Scanner" Im currently drawing a circle on it. the component can be added to a winform and using docking will resize when the winform resizes. At the moment I'm getting the size of the component to calculate the size of the circle but what I want to do is basically say no matter what size the component is the "canvas" is always 300 x 300 wide, so I can say the circle should be positioned at 25,25 with a size of 250x250.

As you might guess from the name "Scanner" I want to plot points on it, but these will be calculated from the center (150,150) location.

Below is the code I have that basically draws the circle.

Many thanks for any help on this.

public partial class Scanner : Panel
{
    public Scanner() {
        InitializeComponent();
        this.DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Draw(g);
        base.OnPaint(e);
    }
    protected override void OnResize(EventArgs e) {
        Graphics g = this.CreateGraphics();
        Draw(g);
        base.OnResize(e);
    }

    private void Draw(Graphics g) {
        g.Clear(Color.Black);
        g.PageUnit = GraphicsUnit.Pixel;
        Pen green = new Pen(Color.Green);
        Font fnt = new Font("Arial", 10);
        SolidBrush sb = new SolidBrush(Color.Red);

        int pos = (this.Width < this.Height ? this.Width : this.Height) / 2;
        int size = (int)(pos * 1.9);
        pos -= ((int)size / 2);
        g.DrawEllipse(green, pos, pos, size, size);
        g.DrawString(this.Width.ToString(), fnt, sb, new Point(0, 0));
    }
}
Harag
  • 1,532
  • 4
  • 19
  • 31
  • I may be missing something here, but... g.DrawEllipse(green, 25, 25, 250, 250); does what you want...? – Edwin Groenendaal May 26 '11 at 13:42
  • I want to draw the "scanner" on a 300x300 image, then transfer this image over to the actual control on the form, the actual control can be any size (but will always be square) so if the control is 500x500 then the 300x300 image will be enlarged to 500x500. Hope this helps – Harag May 26 '11 at 14:13

1 Answers1

1

Based on your recent comment, I understand you want to do your drawing on a fixed-size canvas, and plot this canvas inside the control, as large as will fit in the control.

Try the code below:

public class Scanner : Panel
{
    private Image _scanner;

    public Scanner()
    {
        this.SetStyle(ControlStyles.ResizeRedraw, true);

        CreateScanner();
    }

    private void CreateScanner()
    {
        Bitmap scanner = new Bitmap(300, 300);
        Graphics g = Graphics.FromImage(scanner);

        g.DrawEllipse(Pens.Green, 25, 25, 250, 250);

        g.Dispose();
        _scanner = scanner;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        int shortestSide = Math.Min(this.Width, this.Height);

        if (null != _scanner)
            e.Graphics.DrawImage(_scanner, 0, 0, shortestSide, shortestSide);
    }

}
Edwin Groenendaal
  • 2,304
  • 16
  • 8
  • And to answer your other question, you don't actually need to derive from Panel or Picturebox, unless you want to use their existing functionality. You can derive from Control just fine. – Edwin Groenendaal May 26 '11 at 14:41