菜鸟学C#

淡泊以明志,宁静以致远

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

新建项目,添加PictureBox,Button控件

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Open photo";
            dlg.Filter = "jpg files (*.jpg)|*.jpg"
                            + "|All files (*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pictureBox1.Image = new Bitmap(dlg.OpenFile());
                }
                catch(ArgumentException ex)
                {
                    MessageBox.Show("Unable to load file:" + ex.Message);
                    pictureBox1.Image = null;
                }
            }
            dlg.Dispose();
        }
    }
}

运行结果:

tmp10B

tmp10C

posted on 2009-09-03 12:35  东东会会  阅读(820)  评论(0编辑  收藏  举报