获取图片:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
pictureBox2.Image = GetFile(fullpath);
}
// 将内存流转为图片
private Image GetFile(string path)
{
MemoryStream stream = ReadFile(path);
return stream == null ? null : Image.FromStream(stream);
}
// 将文件转为内存流
private MemoryStream ReadFile(string path)
{
if (!File.Exists(path))
return null;
using (FileStream file = new FileStream(path, FileMode.Open))
{
byte[] b = new byte[file.Length];
file.Read(b, 0, b.Length);
MemoryStream stream = new MemoryStream(b);
return stream;
}
}
不显示并释放资源
if (result == DialogResult.OK)
{
Image image = pictureBox2.Image;
pictureBox2.Image = null;
image.Dispose();
}