高手!在C#中如何利用picturebox来显示图片?

我想利用visual studio 平台编写一个小程序,用c#来实现,但是遇到了困难,如何利用picturebox来显示?我想设置一个按钮,单击时让picturebox显示图片。要怎么办啊?高手求救一下!在输入图片路径时出错,要怎么解决呢?(要详细代码!)

(1)新建一个C#窗体项目,项目名为showPicture,在Form1上添加一个Picturebox控件和两个按钮。

(2)添加代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace showPicture

{

public partial class Form1:Form

{

public Form1()

{

InitializeComponent();

}

private string pathname=string.Empty;//定义路径名变量

private void button1_Click(object sender,EventArgs e)//打开方法

{

OpenFileDialog file=new OpenFileDialog();

file.InitialDirectory=".";

file.Filter="所有文件(*.*)|*.*";

file.ShowDialog();

if(file.FileName!=string.Empty)

{

try

{

pathname=file.FileName;//获得文件的绝对路径

this.pictureBox1.Load(pathname);

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

private void button2_Click(object sender,EventArgs e)//保存方法

{

SaveFileDialog save=new SaveFileDialog();

save.ShowDialog();

if(save.FileName!=string.Empty)

{

pictureBox1.Image.Save(save.FileName);

}

}

}

}

(3)显示效果

模式显示。

(4)保存方法调用效果。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-10
在Win窗体放三个控件
"Button1" "picturebox1" 和对话框"openFileDialog1"
然后写"Button"的Onclick事件
DialogResult result = openFileDialog1.ShowDialog();//声明一个对话框
if (result == DialogResult.OK)//判断对话框是否被选中
{
pictureBox1.ImageLocation = openFileDialog1.FileName;//pictureBox显示被对话框选中的图片
}

可以设置openFileDialog1的Filer属性来设置选择图片的格式
希望你能满意
第2个回答  2017-08-10
pictureBox1.Image的获得图片路径的三种方法
1.绝对路径: this.pictureBox1.Image=Image.FromFile("D:\\001.jpg");

2.相对路径: Application.StartupPath;
可以得到程序根目录
this.pictureBox1.Image=Image.FromFile(Application.StartupPath "\\1.gif");

3.获得网络图片的路径
this.pictureBox1.Image=Image.FromStream(System.Net.WebRequest.Create(http://www.域名名称.com/logo.gif).GetResponse().GetResponse().GetResponseStream());
第3个回答  推荐于2017-06-09
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

Image img = Image.FromFile(@"F:\picture\q.jpg");//双引号里是图片的路径

pictureBox1.Image = img;
}

}
}本回答被网友采纳
第4个回答  推荐于2017-10-14
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image img = System.Drawing.Image.FromFile( this.openFileDialog1.FileName);//双引号里是图片的路径
pictureBox1.Image = img;
}
这样改可以自己打开图片位置本回答被网友采纳
相似回答