选择图像后,在图片框内显示相同的图像

时间:2013-11-30 18:06:29

标签: c# .net winforms

点击按钮我正在从文件系统中取出图像并保存到数据库中,一切正常,但我想选择图像将图像显示到pictureBox 1

 OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
   if (open.ShowDialog() == DialogResult.OK)
   {
       txtPhoto.Text = open.FileName;
   }

   string image = txtPhoto.Text;
   Bitmap bmp = new Bitmap(image);
   FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
   byte[] bimage = new byte[fs.Length];
   fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
   fs.Close();

   byte[] Photo = bimage;

4 个答案:

答案 0 :(得分:1)

简单代码:

picturebox.Image = Bitmap.FromFile(yourimagepath);

答案 1 :(得分:1)

您可以使用Image属性为PictureBox控件设置图片。

试试这个:

        DialogResult result= openFileDialog1.ShowDialog();
        if(result==DialogResult.OK)
            pictureBox1.Image =new Bitmap(openFileDialog1.FileName);

如果您想在代码中添加

完整代码:

       OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
       if (open.ShowDialog() == DialogResult.OK)
       {
           txtPhoto.Text = open.FileName;
       }

       string image = txtPhoto.Text;
       Bitmap bmp = new Bitmap(image);
       pictureBox1.Image = bmp;//add this line
       FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
       byte[] bimage = new byte[fs.Length];
       fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
       fs.Close();

       byte[] Photo = bimage;

答案 2 :(得分:0)

OpenFileDialog open = new OpenFileDialog()
        {
            Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
        };

        if (open.ShowDialog() == DialogResult.OK)
        {
            PictureBoxObjectName.Image = Image.FromFile(open.FileName);
        }

答案 3 :(得分:0)

openFileDialog1.Multiselect = false;
openFileDialog1.Filter= "jpg files (*.jpg)|*.jpg";  

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{             
    foreach (String file in openFileDialog1.FileNames)
    {
       picturebox1.Image = Image.FromFile(File);
    }
}

这是为了选择一张图像。