您的位置:首页 > 编程语言 > C#

.C# 拷贝一个图片到指定文件夹下(IO文件操作实例)

2011-12-16 17:40 866 查看
string picPath = "";//图片路径作为全局变量,是因为图片上传的时候更改了图片的路径,所以要作为全局变量,以便此时图片的路径是拷贝后的地方。
OpenFileDialog ofd = new OpenFileDialog();
private void btnUpload_Click(object sender, System.EventArgs e)
{
//图片上传对话框

//OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "浏览";
ofd.Filter = "JPEG|*.jpeg;*.jpg|Bitmap|*.bmp;*.dib|GIF|*.gif;*.gfa|Portable Network Graphics|*.png|Windows Metafile|*.wmf|Windows Enhanced Metafile|*.emf";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (ofd.FileName != null)
{
this.imgPicture.Image = Image.FromFile(ofd.FileName);
this.txtPicturePath.Text = ofd.FileName; //上传图片路径

//转换为byte类型,保存

MemoryStream ms = new MemoryStream();
this.imgPicture.Image.Save(ms, imgPicture.Image.RawFormat);
image = null;
image = ms.ToArray(); //赋值byte

ms.Close();

picPath = ofd.FileName; //获得选择的文件的路径

string filename = Path.GetFileName(picPath); //获得图片的真实名字
string targetPath = @"E:\SEA\Magzine\pic\"+filename;
if (!System.IO.Directory.Exists(@"E:\SEA\Magzine\pic"))
{ // 目录不存在,建立目录
System.IO.Directory.CreateDirectory(@"E:\SEA\Magzine\pic");
}
System.IO.File.Copy(ofd.FileName,targetPath);
picPath = targetPath;
}
}

if (picPath.Equals(""))
{
MessageBox.Show("请上传图片!!!");
}

这样即可以把图片上传显示出来,并且把图片拷贝到了E:\SEA\Magzine\pic目录下,且数据库中也存储的是拷贝后的图片的路径。

string path=System.Windows.Forms.Application.StartupPath + @http://www.cnblogs.com/../;

System.IO.Directory.SetCurrentDirectory(path);

FileInfo OldFileName = new FileInfo(System.IO.Directory.GetCurrentDirectory() + @"\images\1.gif");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐