怎么保存图片到硬盘上?

在项目里需要在页面中增加一个按钮,然后通过它打开硬盘上的图片,并且保存到相应的目录中。这样,我就建了一个Form,如图所示:
在这里插入图片描述
可以看到它有2个Button, 分别是用来打开目录里的图片文件,然后保存到相应的目录中。
另外,我也从控件中拖入了openFileDialog和saveFileDialog这2个控件,其中主要用到了openFileDialog1,saveFileDialog1作为保留。
在当中是一个pictureBox控件,它的SizeMode属性设为Zoom,也就是自动适配原图。
由于需要在调用该Form的时候传入2个值,所以在构造函数中建了2个参数用来传值。
好了,上代码了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HL_Dev_Template
{
    public partial class FormUploadPic : Form
    {
        private string selectedWp;
        private int wpType;

        public FormUploadPic(string s, int t)
        {
            this.selectedWp = s;
            this.wpType = t;
          
            InitializeComponent();
            initDialog();
        }

        private void FormUploadPic_Load(object sender, EventArgs e)
        {

        }

        public void initDialog()
        {
            openFileDialog1.InitialDirectory = @"C:\";//设置起始文件夹
            openFileDialog1.Filter = "图片文件 (*.jpg)|*.jpg";//设置文件筛选类型
            openFileDialog1.FileName = "";//设施初始的文件名为空
            openFileDialog1.CheckFileExists = true;//检查文件是否存在
            openFileDialog1.CheckPathExists = true;//检查路径是否存在

            saveFileDialog1.InitialDirectory = Application.StartupPath + "\\Img\\";
            saveFileDialog1.Filter = "图片文件 (*.jpg)|*.jpg";
            openFileDialog1.FileName = "文件名";
        }

        private void button_open_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();//显示对话框接返回值
            if (result == DialogResult.OK)
            {
                pictureBox1.Image = System.Drawing.Image.FromFile(openFileDialog1.FileName);
            }
        }

        private void buttonSaveFile_Click(object sender, EventArgs e)
        {
            string pictureName = "";
            switch (this.wpType)
            {
                case 1:  // 点 
                    pictureName = saveFileDialog1.InitialDirectory + this.selectedWp + "_p.jpg";
                    break;
                case 2:  //边
                    pictureName = saveFileDialog1.InitialDirectory + this.selectedWp + "_e.jpg";
                    break;
                case 3: //扫描节点
                    pictureName = saveFileDialog1.InitialDirectory + this.selectedWp + "_s.jpg";
                    break;
            }
             //图片另存
            using (MemoryStream mem = new MemoryStream())
            {
                try
                {
                    Bitmap bmp = new Bitmap(pictureBox1.Image);
                    //保存到磁盘文件
                    bmp.Save(@pictureName, pictureBox1.Image.RawFormat);
                    bmp.Dispose();
                    MessageBox.Show("图片保存成功!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch ( Exception err)
                {
                    MessageBox.Show("图片保存不成功!\r\n" + err.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                
            }
        }
    }
}

运行的效果如下图所示:
在这里插入图片描述
保存到指定的目录里了。
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值