(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)保存方法调用效果。
在Win窗体放三个控件
"Button1" "picturebox1" 和对话框"openFileDialog1"
然后写"Button"的Onclick事件
DialogResult result = openFileDialog1.ShowDialog();//声明一个对话框
if (result == DialogResult.OK)//判断对话框是否被选中
{
pictureBox1.ImageLocation = openFileDialog1.FileName;//pictureBox显示被对话框选中的图片
}
可以设置openFileDialog1的Filer属性来设置选择图片的格式
希望你能满意
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());
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;
}
}
}
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.Drawing.Image img = System.Drawing.Image.FromFile( this.openFileDialog1.FileName);//双引号里是图片的路径
pictureBox1.Image = img;
}
这样改可以自己打开图片位置