Winfrom开发之OpenFileDialog选中图片即时保存至项目中

openFileDialog1.Filter = "图片|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Image img = Image.FromFile(openFileDialog1.FileName);
//文件名
string filename = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\") + 1);
//文件保存文件夹路径,此处【 Application.StartupPath 】就是根目录
//string savepath = Application.StartupPath + "\\img\\";
#region 保存路径为项目的根目录,从debug目录往上截取两级文件夹
string rootpath = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\"));
rootpath = rootpath.Substring(0, rootpath.LastIndexOf("\\"));
//文件保存文件夹路径
string savepath = rootpath + "\\img\\";
#endregion
//文件保存路径+文件名
string imgSavepath = savepath + filename;

//判断是否存在img文件夹
if (Directory.Exists(savepath))
{
//存在img文件夹
//判断该路径下是否已经存在同名文件
if (File.Exists(imgSavepath))
{
//提示是否覆盖
if (DialogResult.Yes != MessageBox.Show("图片已存在!", "该图片已存在,是否覆盖原图片?", MessageBoxButtons.YesNo))
{
//点击【否】,返回,取消操作。
return;
}
}
}
else
{
//不存在,在根目录下创建img文件夹
Directory.CreateDirectory(savepath);
}
try
{
Image im = img;
Bitmap bit = new Bitmap(im);
bit.Save(imgSavepath, System.Drawing.Imaging.ImageFormat.Bmp);
MessageBox.Show("图片保存成功!");
}
catch{}

转载于:https://www.cnblogs.com/Julyzjc/p/9318448.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值