tags:

views:

22

answers:

2

I have a picturebox that is taking up 512x512. That is way to big, i can make it 256x1024 which is preferable but then how do i make it scrollable?

A: 

http://www.c-sharpcorner.com/UploadFile/mgold/ScrollablePictureBox09142005050421AM/ScrollablePictureBox.aspx

bugtussle
That code is fucking awful. It flickers likes crazy, doesnt scroll properly and has other problems.
acidzombie24
That is more of a note to who is reading. I posted my solution after asking another question on SO and by guessing what i need to do. It works very well.
acidzombie24
+1  A: 
class ScrollablePictureBox : PictureBox
{
    public PictureBox pic = new PictureBox();
    public PictureBox picCorner = new PictureBox();
    HScrollBar hScroll = new HScrollBar();
    VScrollBar vScroll = new VScrollBar();
    int w, h, scrollAmount;
    public ScrollablePictureBox(int w_, int h_, Image img, int scrollAmount)
    {
        AddControls();
        Set(w_, h_, img, scrollAmount);
    }
    public ScrollablePictureBox()
    {
        AddControls();
    }
    public void ScrollEvent(object sender, MouseEventArgs e)
    {
        if (e.Delta == 0)
            return;

        var v = vScroll.Value + (e.Delta >= 120 ? -scrollAmount : scrollAmount);
        v = v > vScroll.Maximum ? vScroll.Maximum : v;
        v = v < vScroll.Minimum ? vScroll.Minimum : v;
        vScroll.Value = v;
    }
    void AddControls()
    {
        picCorner.BackColor = Color.Gray;
        vScroll.ValueChanged += Vert_EventHandler;

        this.Controls.Add(picCorner);
        this.Controls.Add(vScroll);
        this.Controls.Add(hScroll);
        this.Controls.Add(pic);            
    }
    public void Set(int w_, int h_, Image img, int scrollAmount_)
    {
        w = w_;
        h = h_;
        scrollAmount = scrollAmount_;
        pic.Image = img;
        pic.Width = img.Width;
        pic.Height = img.Height;

        if (w >= img.Width)
        {
            this.Width = w;
            hScroll.Visible = false;
        }
        else
        {
            hScroll.Left = 0;
            hScroll.Top = h;
            hScroll.Width = w;
            hScroll.Minimum = 0;
            hScroll.Maximum = pic.Image.Width - w;
            hScroll.Scroll += hScrollBar1_Scroll;
            hScroll.Visible = true;
        }

        if (h >= img.Height)
        {
            this.Width = w;
            vScroll.Visible = false;
        }
        else
        {
            vScroll.Left = w;
            vScroll.Top = 0;
            vScroll.Height = h;
            vScroll.Minimum = 0;
            vScroll.Maximum = pic.Image.Height - h;
            vScroll.Scroll += vScrollBar1_Scroll;
            vScroll.Visible = true;
        }

        this.Width = vScroll.Visible ? w + vScroll.Width : w;
        this.Height = hScroll.Visible ? h + hScroll.Height: h;

        picCorner.Left = vScroll.Left;
        picCorner.Width = vScroll.Width;
        picCorner.Top = hScroll.Top;
        picCorner.Height = hScroll.Height;
        picCorner.BackColor = this.BackColor;
        picCorner.Visible = (vScroll.Visible && hScroll.Visible);
    }
    void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        pic.Left = -e.NewValue;
    }
    void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        pic.Top = -e.NewValue;
    }
    void Vert_EventHandler(object sender, EventArgs e)
    {
        pic.Top = -vScroll.Value;
    }
}
acidzombie24