25

I want to be able to use a Panel or similar to draw graphics onto a Winform. I cannot seem to see anything regarding adding scrollbars if the graphics become larger than the control?

Is it possible to do this with a Panel or is there a similar control that will allow it?

Darren Young
  • 10,972
  • 36
  • 91
  • 150

2 Answers2

27

Set the AutoScroll property to true and the AutoScrollMinSize property to the size of the image. The scrollbars will now automatically appear when the image is too large.

You'll want to inherit your own class from Panel so that you can set the DoubleBuffered property to true in the constructor. Flicker would be noticeable otherwise. Some sample code:

using System;
using System.Drawing;
using System.Windows.Forms;

class ImageBox : Panel {
    public ImageBox() {
        this.AutoScroll = true;
        this.DoubleBuffered = true;
    }
    private Image mImage;
    public Image Image {
        get { return mImage; }
        set {
            mImage = value;
            if (value == null) this.AutoScrollMinSize = new Size(0, 0);
            else {
                var size = value.Size;
                using (var gr = this.CreateGraphics()) {
                    size.Width = (int)(size.Width * gr.DpiX / value.HorizontalResolution);
                    size.Height = (int)(size.Height * gr.DpiY / value.VerticalResolution);
                }
                this.AutoScrollMinSize = size;
            }
            this.Invalidate();
        }
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
        base.OnPaint(e);
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks Hans. I'll give this a go now. – Darren Young Nov 30 '10 at 10:27
  • Sorry Hans....the image that will fit into the panel is created dynamically at run time. The graphics are created depending upon certain parameters. Would this still work? – Darren Young Nov 30 '10 at 10:28
  • 1
    Yah, assign the Image property. Works as well at design time as at run time. – Hans Passant Nov 30 '10 at 10:39
  • The way I create the image is using graphics to draw circles on a panel. Would i save this panel as an image? Sorry the maybe silly questions - i'm totally new to graphics/image programming. Thanks again. – Darren Young Nov 30 '10 at 10:41
  • 1
    Just do the drawing in the OnPaint() method. The TranslateTransform call there makes sure that your drawing is properly scrolled. You will however have to set the AutoScaleMinSize property yourself. – Hans Passant Nov 30 '10 at 10:47
  • Maybe something has changed between when this question was asked and now, but how come even the base Panel object does not have the DoubleBuffered property for me to access in Visual Studio Intellisense? I'm using VS2012 and .NET 4.5 if that matters, and I can still set AutoScroll. – Wolfman2000 Dec 30 '12 at 15:57
  • Shows up just fine when I try it. Clearly you'll need to click the Ask Question button and document your question properly. – Hans Passant Dec 30 '12 at 16:00
  • I have a problem - I am trying to display 1024x768 jpg using your code in 512x512 panel, unfortunately when I scroll I cannot scroll to the end of image (horizontally and vertically). Any ideas? – Siegfried Apr 15 '14 at 21:56
0

I'm not 100% sure what you're trying to accomplish, but here is a similar SO question that might help you.

You could also try using a PictureBox that you would manually change its size as the graphics get larger. Then set your form AutoScroll to true.

Community
  • 1
  • 1
msergeant
  • 4,771
  • 3
  • 25
  • 26
  • I would not use the picturebox as a canvas for drawing. The picturebox is only for displaying pictures. See i.e. Bob Powell's article regarding this exact issue. – Pedery Nov 29 '10 at 18:08