C#图片读取和保存

时间:2020-12-29 17:31:31
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
            OpenFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|bmp文件(*.bmp)|*.bmp|ico文件(*.ico)|*.ico|png文件(*.png)|*.png|gif文件(*.gif)|*.gif";
     if (OpenFileDialog1 .ShowDialog() == DialogResult.OK)
      { Bitmap image = new Bitmap(OpenFileDialog1 .FileName);
         pictureBox1.Image = image;
   }
 }

        private void button2_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image==null)
            {
                MessageBox.Show("没有加载图片", "错误!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
            string str;
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
            SaveFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|bmp文件(*.bmp)|*.bmp|ico文件(*.ico)|*.ico|png文件(*.png)|*.png|gif文件(*.gif)|*.gif";
            SaveFileDialog1.ShowDialog();
            str = SaveFileDialog1.FileName;
            box1.Save(str);

        }
        }

        }
    }