WinForm PictureBox的简单使用

2021-02-18  本文已影响0人  在凌晨失了眠

新建窗体,添加一个按钮、OpenFileDialog、picturebox。
点击按钮打开文件选择对话框,选中图片,点击确认显示图片。

        private void btn_test_Click(object sender, EventArgs e)
        {
            //指定查找的文件类型
            openFileDialog1.Filter = "@.Jpg|*.jpg|@.Png|*.png|@.Gif|*.gif|@.All files|*.*";
            //该对话框会返回一个DialogResult类型的值,DialogResult.OK或者DialogResult.NO
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //通过输入文件目录,文件模式,访问模式等参数,通过流打开文件
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                //通过调用系统的画笔工具,画出一个Image类型的数据,传给pictureBox。
                Image im = Image.FromStream(fs);
                pictureBox1.Image = im;
            }
        }
上一篇 下一篇

猜你喜欢

热点阅读