Roger Luo

超越梦想一起飞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# PictureBox 使用心得

Posted on 2013-03-02 18:36  Roger Luo  阅读(7192)  评论(0编辑  收藏  举报

Synchronously loading image

PictureBox pbx = new PictureBox();
pbx.Image = Image.FromFile(filename);
or
PictureBox pbx = new PictureBox();
pbx.Load(filename); // filename could be the url file://filename
 
Asynchronously loading image
private void btnOpen_Click(object sender, EventArgs e)
{
PictureBox pbxPic= new PictureBox();
pbxPic.WaitOnLoad = false;
pbxPic.LoadAsync(url or filename);
}
private void pbxPic_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
this.stsStatus.SuspendLayout();

this.stsStatus.Items["tsslblLocation"].Text = pbxPic.ImageLocation;

this.stsStatus.ResumeLayout(false);
this.stsStatus.PerformLayout();
}
private void pbxPic_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
{
ToolStripProgressBar pgb = this.stsStatus.Items["tsspgbLoading"] as ToolStripProgressBar;
if (pgb != null)
{
pgb.Value = e.ProgressPercentage;
}
}

Show Image in picturebox

The simple one is use PictureBoxSizeMode.Zoom enum to make the image fit to the picturebox in radio

If you want to show the scroll bar, you should add picturebox into a panel. And then set panel’ autoscroll as true and set picturebox’s sizemode as PictureBoxSizeMode as Auto.