如何使用OpenFileDialog设置PictureBox控件的Image属性? C#winforms

时间:2011-01-22 10:05:11

标签: c# winforms

如何使用OpenFileDialog设置PictureBox控件的Image属性?

1 个答案:

答案 0 :(得分:4)

使用PictureBox.Image

设置Image.FromFile
OpenFileDialog _openFileDialog = new OpenFileDialog();
_openFileDialog.FileOk += OpenFileDialogFileOk;
_openFileDialog.CheckFileExists = true;
_openFileDialog.CheckPathExists = true;
_openFileDialog.ShowDialog();

void OpenFileDialogFileOk(object sender, CancelEventArgs e)
{
    var imageInformation = new FileInfo(_openFileDialog.FileName);
    var sizeInBytes = imageInformation.Length;

    myPictureBox.Image = Image.FromFile(_openFileDialog.FileName);
}