PictureBox

利用PictureBox和Panel实现使用滚动条浏览大图片

View Code
 1 由于Picturebox控件在显示图片时不能直接使用滚动条,所以必须借助Panel控件实现以滚动条的方式浏览大图片。具体操作步骤如下:

 2 

 3   (1)新建一个Windows应用程序,命名为“scrollBar”,调整窗体到适当大小。更改“Form1.cs”为“FormScrollBar.cs”。

 4 

 5   (2)切换到代码方式,添加名称空间引用:

 6 

 7   using System.Drawing.Drawing2D;

 8 

 9   (3) 在窗体上分别添加一个button控件命名为“buttonOpenFile”,一个openFileDiolog控件,Picturebox和Panel控件各一个,将Panel控件的AutoScroll属性设为true,pictureBox的SizeMode属性设置为AutoSize。。注意这里pictureBox在Panel内部,但是pictureBox的Dock不能设置成Fill!!!

10 

11   (4) 在“buttonOpenFile”控件的click事件中添加如下代码:

12 

13 以下是引用片段:

14   private void buttonOpenFile_Click(objectsender,EventArgse)

15   {

16   openFileDialog1.ShowDialog();

17   if(openFileDialog1.FileName.Trim()=="")

18   return;

19   try

20   {

21   Bitmap mybitmap=new Bitmap(openFileDialog1.FileName);

22   pictureBox1.Image=mybitmap;

23   }

24   catch(Exception Err)

25   {

26   MessageBox.Show("打开文件错误!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

27   }

28   }

 

PictureBox 异步加载图片

View Code
 1  private void button1_Click(object sender, EventArgs e) 

 2         { 

 3              openFileDialog1.Filter = "*.JPG|*.GIF|*.BMP"; 

 4              if (openFileDialog1.ShowDialog() == DialogResult.OK) 

 5              { 

 6                  string fullpath = openFileDialog1.FileName; 

 7                  //图片异步加载完成后的处理事件 

 8                  pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted); 

 9                  //图片加载时,显示等待光标 

10                  pictureBox1.UseWaitCursor = true; 

11                  //采用异步加载方式 

12                  pictureBox1.WaitOnLoad = false; 

13                  Bitmap bm = new Bitmap(fullpath); 

14                  Point pt = new Point(bm.Size ); 

15                  if (pt.X > pictureBox1.Size.Width || pt.Y > pictureBox1.Size.Height) 

16                  { 

17                      pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; 

18                  } 

19                  else 

20                  { 

21                      pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 

22                  } 

23                  //开始异步加载,图片的地址,请自行更换 

24                 pictureBox1.LoadAsync(fullpath ); 

25              } 

26         } 

27         void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) 

28         { 

29             //图片加载完成后,将光标恢复 

30             pictureBox1.UseWaitCursor = false; 

31         }

 

 

你可能感兴趣的:(BO)