1 创建窗体项目,添加一个pictureBox1控件和一个openFileDialog1控件
//添加引用using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace View
{
public partial class OtherForm : Form
{
public OtherForm()
{
InitializeComponent();
}
//定义一组图片变量
byte[] picturebytes;
private void upfile_Click(object sender, EventArgs e)
{
//定义打开文件的类型,图片格式
openFileDialog1.Filter = "**jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
//获取打开图片返回值
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
picturebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
picturebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
MemoryStream ms = new MemoryStream(picturebytes);
Bitmap img = new Bitmap(ms);
pictureBox1.Image = img;
}
else
{
MessageBox.Show("图片打开失败");
}
}
}
}
2 测试