public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void import_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp"; if (openfile.ShowDialog() == DialogResult.OK) { //第一步,打开图片文件,获得比特流,生成字节数组。 byte[] picbinary = GetFileBytes(openfile.FileName); //第二步,将比特流存在内存工作流中 MemoryStream mempicstream = new MemoryStream(picbinary); //加载内存流到图片控件 this.pictureBox1.Image = Image.FromStream(mempicstream); mempicstream.Dispose(); mempicstream.Close(); } } public byte[] GetFileBytes(string Filename) { if (Filename == "") return null; try { FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read); BinaryReader binaryReader = new BinaryReader(fileStream); byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length); binaryReader.Close(); return fileBytes; } catch { return null; } } }
显示完整图片:
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;