0

I have implemented a picturebox in my in my form.I even implemented the scrollbars to make the image fit in it.so now the problem is when i try to scroll the button down,it scrolls down and immediately when i leave the mouse the button scrolls up ..here is the code implemnted please give some suggestions..

     public void DisplayScrollBars()
    {
        // If the image is wider than the PictureBox, show the HScrollBar.
        if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
        {
            hScrollBar1.Visible = false;
        }
        else
        {
            hScrollBar1.Visible = true;
        }

        // If the image is taller than the PictureBox, show the VScrollBar.
        if (pictureBox1.Height >
            pictureBox1.Image.Height - this.hScrollBar1.Height)
        {
            vScrollBar1.Visible = false;
        }
        else
        {
            vScrollBar1.Visible = true;
        }
    }

    private void HandleScroll(Object sender, ScrollEventArgs se)
    {
        /* Create a graphics object and draw a portion 
           of the image in the PictureBox. */
        Graphics g = pictureBox1.CreateGraphics();

        g.DrawImage(pictureBox1.Image,
          new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width,
          pictureBox1.Bottom - hScrollBar1.Height),
          new Rectangle(hScrollBar1.Value, vScrollBar1.Value,
          pictureBox1.Right - vScrollBar1.Width,
          pictureBox1.Bottom - hScrollBar1.Height),
          GraphicsUnit.Pixel);


        pictureBox1.Update();
    }


    public void SetScrollBarValues()
    {
        // Set the Maximum, Minimum, LargeChange and SmallChange properties.
        this.vScrollBar1.Minimum = 0;
        this.hScrollBar1.Minimum = 0;


        // If the offset does not make the Maximum less than zero, set its value. 
        if ((this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
        {
            this.hScrollBar1.Maximum =
                this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
        }
        // If the VScrollBar is visible, adjust the Maximum of the 
        // HSCrollBar to account for the width of the VScrollBar.  
        if (this.vScrollBar1.Visible)
        {
            this.hScrollBar1.Maximum += this.vScrollBar1.Width;
        }
        this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
        this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;


        // Adjust the Maximum value to make the raw Maximum value 
        // attainable by user interaction.
        this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;

        // If the offset does not make the Maximum less than zero, set its value.    
        if ((this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
        {
            this.vScrollBar1.Maximum =
                this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
        }


        // If the HScrollBar is visible, adjust the Maximum of the 
        // VSCrollBar to account for the width of the HScrollBar.
        if (this.hScrollBar1.Visible)
        {
            this.vScrollBar1.Maximum += this.hScrollBar1.Height;
        }
        this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
        this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;

        // Adjust the Maximum value to make the raw Maximum value 
        // attainable by user interaction.
        this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
    }

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
        // If the PictureBox has an image, see if it needs 
        // scrollbars and refresh the image. 
        if (pictureBox1.Image != null)
        {
            this.DisplayScrollBars();
            this.SetScrollBarValues();
            this.Refresh();
        }
    }
raghu
  • 431
  • 2
  • 11
  • 23
  • 2
    can't you just put your `pictureBox` in a `panel` and set the `panel.AutoScroll=true` ? – Bolu Jul 06 '11 at 10:51
  • Ya every one said that ,but when load the picture it was not able scroll,it is showing only part of the image.so,i implemented scroll bars. – raghu Jul 06 '11 at 13:51
  • you need to change `pictureBox.SizeMode` to `AutoSize`. check my answer.. – Bolu Jul 06 '11 at 14:02

1 Answers1

0

As I comment above, the correct way of doing this is to put your pictureBox in a panel and set the panel.AutoScroll=true. Also you need to set pictureBox.SizeMode=AutoSize so it is sized equal to the size of the image that it contains. Check: PictureBoxSizeMode Enumeration

Bolu
  • 8,696
  • 4
  • 38
  • 70
  • Its not working that way!i tried it lot of times and even now!!the problem is the picture is not able to scroll.and only part of the image loads(i.e depending on the size of picture box). – raghu Jul 07 '11 at 10:14
  • @raghu, I'm afraid you did something wrong, as it does work for me (and for others). you can start with a new project and 1. put a `panel`; 2. set `panel.AutoScroll=true`; 3. put a `pictureBox` inside the `panel`; 4. set `pictureBox.SizeMode=AutoSize`; 5. run the program – Bolu Jul 07 '11 at 11:14