1. 程式人生 > >在PictureBox中打開圖片文件

在PictureBox中打開圖片文件

ret one bmp byte color length ebo pan vat

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中打開圖片文件