C# .NET - picturebox

Asked By Pinakin Patel on 31-Mar-07 05:50 AM

i want to insert horizontal and verticar bar in picturebox

how can i?

Panel with AutoScroll =True

Virgil Hretcanu replied to Pinakin Patel on 31-Mar-07 08:18 PM
Pinakin, why don't you use a panel ... set its AutoScroll property to True, and place your PictureBox in it. Also you may want to set picturebox's SizeMode to AutoSize.

scrollbar to picturebox control

sundar k replied to Pinakin Patel on 31-Mar-07 09:48 PM

The following code example adds horizontal and vertical scroll bars to a PictureBox control, and loads an Image into the picture box on the DoubleClick event of the control.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace Scrollbar
{
    public class Form1 :  Form
   {
      private  OpenFileDialog openFileDialog1;
      private  PictureBox pictureBox1;
      private  VScrollBar vScrollBar1;
      private  HScrollBar hScrollBar1;
   
      public Form1()
      {
         InitializeComponent();
      }
   
      private void InitializeComponent()
      {
         this.openFileDialog1 = new OpenFileDialog();
         this.pictureBox1 = new PictureBox();
         this.vScrollBar1 = new VScrollBar();
         this.hScrollBar1 = new HScrollBar();
         this.pictureBox1.SuspendLayout();
         this.SuspendLayout();
       
         this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
         this.pictureBox1.Controls.AddRange(
             new Control[] { this.vScrollBar1, this.hScrollBar1});
         this.pictureBox1.Dock = DockStyle.Fill;
         this.pictureBox1.Size = new System.Drawing.Size(440, 349);
         this.pictureBox1.DoubleClick += new EventHandler(this.pictureBox1_DoubleClick);

         this.vScrollBar1.Dock = DockStyle.Right;
         this.vScrollBar1.Location = new System.Drawing.Point(422, 0);
         this.vScrollBar1.Size = new System.Drawing.Size(16, 331);
         this.vScrollBar1.Visible = false;
         this.vScrollBar1.Scroll +=
             new ScrollEventHandler(this.HandleScroll);
        
         this.hScrollBar1.Dock = DockStyle.Bottom;
         this.hScrollBar1.Location = new System.Drawing.Point(0, 331);
         this.hScrollBar1.Size = new System.Drawing.Size(438, 16);
         this.hScrollBar1.Visible = false;
         this.hScrollBar1.Scroll +=
             new ScrollEventHandler(this.HandleScroll);
        
         this.ClientSize = new System.Drawing.Size(440, 349);
         this.Controls.AddRange(new Control[] {this.pictureBox1});
         this.Text = "Form1";
         this.Resize += new System.EventHandler(this.Form1_Resize);
         this.pictureBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }

      [STAThread]
      static void Main()
      {
         Application.Run(new Form1());
      }

private void pictureBox1_DoubleClick (Object sender, EventArgs e)
{
   // Open the dialog box so the user can select a new image.
   if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
   {
      // Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
      this.DisplayScrollBars();
      this.SetScrollBarValues();
   }
 }
 
private void Form1_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();
   }
}
 
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;
   }

  }
}

you can also refer to below msdn link for more info,
http://msdn2.microsoft.com/en-us/library/system.windows.forms.scrollbar.aspx

Thanks

dominic tobias replied to sundar k on 08-Sep-08 06:39 PM
Thanks the code worked perfectly straight off. Smooth scrolling too - good job =)
better to use picture box resize event.
Bakul Ranawat replied to sundar k on 17-Dec-08 07:57 AM
Hey man thanks for the code it was lot helpful, but i have come across one problem i.e while maximizing and minimizing. The problem is the form size change event is called before the size change of picture box creating a trouble( the previous image rectangle is scrolling over the new image(bigger image)). I have found a solution for it and it worked for me. It would be better if we use picture box re size event like the following instead of form re size event.


        private void OnPictureBox1Resize(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();
            }
        }
Umesh replied to Bakul Ranawat on 14-Aug-10 09:42 AM
Thanks a lot,
this article helps me a lot for my project
ramin ab replied to Bakul Ranawat on 21-Apr-13 09:21 AM
end of post
ramin ab replied to Bakul Ranawat on 21-Apr-13 09:21 AM
end of post
ramin ab replied to Bakul Ranawat on 21-Apr-13 09:21 AM
end of post
help
that is not functional. please help me out with this. Thanks TAke the controls on panel and verticale scrollbar. . and set the height value of vertical scrollbar > the height of panel control After you place an HScrollBar control on a Form, the next step is to this value, we apply on other controls. For example, we can implement scrolling in a PictureBox control by displaying image in a PictureBox again on the scroll event bar. The following code snippet adds an event handler for As New HScrollBar The following code snippet is used to redraw the image on a PictureBox based on the value of the HScrollBar. Private Sub hScroller_Scroll( ByVal sender As System. Object
I'm working on in MS Visual Studio, I have a form that contains a panel. The panel has a custom paint event that uses Graphics.DrawLine and similar functions to create an based on a mathematical function - - in this case, it's drawing a timeline in the panel with little vertical ticks to note each hour. This timeline panel is contained in a smaller panel and therefore may has a scrollbar that the user can use to look at the part of the timeline that's the edges of what's actually visible right this second? that was loaded on a panel instead of the .Graphics element of a given control. I just drew the whole image on the PictureBox and did not react to the paint event of the PictureBox. . . .but I'll keep
Hi, I am using a panel in my winform application and i want to increase the width of vertical scrollBar. How to increase the Width of vertical ScrollBar of a Panel in WinForm You have to create scroll bar control for that as there is no other option to set some properties of the scrollbar. You can create the Scrollbar Graphics like : • Up arrow image • Down arrow image • Thumb top image • Thumb top span image
System.Windows.Forms.Button btnAnswer; private System.Windows.Forms.CheckBox chkAutoAnswer; private System.Windows.Forms.PictureBox picLogo; private System.Windows.Forms.Label lblStatus; private System.Windows.Forms.Panel pnlVideoR; private System.Windows.Forms.Panel pnlVideoS; private System.Windows.Forms.PictureBox picVideoR; private System.Windows.Forms.PictureBox picVideoS; private System.Windows.Forms.Label lblVideoR; private System.Windows.Forms.Label lblVideoS; private System Forms.Button(); this.chkAutoAnswer = new System.Windows.Forms.CheckBox(); this.picLogo = new System.Windows.Forms.PictureBox(); this.txtStatus = new System.Windows.Forms.TextBox(); this.lblStatus = new System.Windows.Forms.Label(); this.pnlVideoR = new System.Windows.Forms.Panel(); this.picVideoR = new System.Windows.Forms.PictureBox(); this.pnlVideoS = new System.Windows.Forms.Panel(); this.picVideoS = new System.Windows.Forms.PictureBox(); this
I was wonder if I created a bitmap if I could add it to a Panel? For example: Panel panel = new panel() panel.Width = 10; panel.Height = 10; Bitmap bitmap = new Bitmap( 10, 10); Graphics g = Graphics .FromImage(bitmap); g.FillRectangle Brushes .Black, 0, 0, 10, 10); / / Some how add bitmap to Panel / / I know this isn't correct panel.Image = bitmap; I am creating a graph library
06-Jun-13 10:25 AM I have a web page which contains an Update Panel (Panel 1). In this panel I have a tab strip (ts1) Each tab item in ts1 has its own simple / regular Panel (say Panel 2). Inside Panel 2, we have another Update Panel (say Panel 3). In Panel 3, we have again a tab strip (ts2). Basically we are providing
<td> <asp:panel> < / panel> <asp:label id = "f" runat = "server"> < / td> the above td have panel and lable. . . . .panel values are bind in top of td adn lable value bind in bottom of td like below one use style propery of both control < table width = "100%" > < tr > < td > < asp:Panel id = "pnl1" runat = "server" style = "top:0'" > < / asp:Panel > < asp:Label ID = "lbl1" runat = "server" Text = "test" style = "bottom:0px" > < / asp:Label > < / td > < / tr table > if you want to just label comes below the panel control the use one <BR> between both control as follows <td> <asp:panel> < / panel> <BR> <asp:label id = "f" runat = "server"> < / td> But if you want panel on
from code C# .NET 06-Jun-13 09:59 AM i need to create a panel from code . like panel1, panle2 uisng for loop like for(int e = 0;e< = 2;e++) { Panel panel+e = new Panel(); / / / / this should create as panel1, panel2 etc. } pls help regards deva Use this code- for int e = 0;e< = 2;e++) { Panel panel = new Panel(); panel.Id = (e+1).ToString(); This.Controls.Add(panel); } TRy this and let me know. check
Hi , how to apply the css styles for scrollbar. . ? i have used below css properties: body { scrollbar-track-color:red; scrollbar-face-color:green; scrollbar-highlight-color:FFFFFF; scrollbar-base-color:FFFFFF; scrollbar-darkShadow-color:FFFFFF; scrollbar-shadow-color:FFFFFF; scrollbar-arrow-color:FFFFFF; } its working in IE, but its not working