在PictureBox中打开图片文件
Posted jizhiqiliao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在PictureBox中打开图片文件相关的知识,希望对你有一定的参考价值。
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; } } }
以上是关于在PictureBox中打开图片文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在VS2008中点击按钮可以选择图片文件,图片会显示在PictureBox中
怎么让pictureBox里面的图片自动缩放来符合picturebox的size?
C# winform picturebox控件显示图片问题 怎么使picturebox在登录学号10001时显示本地文件夹里10001.jpg