c#基础 动态打开,显示,保存,另存为 图片

发布时间:2020-12-16编辑:脚本学堂
c#基础篇 动态打开,显示,保存,另存为 图片的例子,有需要的朋友可以参考下。

c#基础篇 动态打开,显示,保存,另存为 图片的例子,有需要的朋友可以参考下。

一、打开:
 

复制代码 代码如下:

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;
using System.IO;

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

        /*用ToolStripMenuItem实现打开本地图片*/

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String picPath;
            //filter写法,"打开对话框下部索要显示内容|真正的过滤后缀名"
            openFileDialog1.Filter = "图片文件(jpg,jpeg,bmp,gif,ico,pen,tif)|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
            openFileDialog1.ShowDialog();
            picPath = openFileDialog1.FileName;
            if(File.Exists(picPath))
            {
                pictureBox1.Image = Image.FromFile(picPath);
            }
        }

    /*用ToolStripMenuItem实现打开本地图片*/

   private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Stream FileStream;
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "保存文件实例";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";

            saveImageDialog.ShowDialog();


            if (saveImageDialog.FileName != null)
            {
                //System.IO.Stream fs = (System.IO.Stream)saveImageDialog.OpenFile();
                //FileStream filestream = new FileStream(openFileDialog1.FileName.ToString(),FileMode.Open,FileAccess.Read);
                String fileName = saveImageDialog.FileName.ToString();

                if(fileName!="" && fileName!=null)
                {

                    /*该字符串中的每个字符都被认为具有数字位置:第一个字符的位置是 1,第二个字符的位置是 2,依此类推。
                      如果未指定第三个参数,将返回从第二个参数指定的位置开始直到字符串结尾的子字符串。
                      如果参数不是字符串类型,将先使用 string() 函数转换为字符串,然后计算该转换的结果。
                      substring("12345",2,3) ---->(234)
                      substring("12345",2) ------>(2345)*/

                    /*此处用fileExtName作为switch语句的开关项,但有编译期错误*/

                    String fileExtName = fileName.Substring(fileName.LastIndexOf(".") , 1).ToString() ;
                    System.Drawing.Imaging.ImageFormat imageformat= null;

                    if(fileExtName!=null)
                    {
                        switch(saveImageDialog.FilterIndex)
                        {
                            case 1:
                               imageformat=System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case 2:
                               imageformat=System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                        }
                        if(imageformat==null)
                        {
                            imageformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                        }

                        try
                        {
                            this.pictureBox1.Image.Save(fileName,imageformat);
                        }
                        catch
                        {
                            MessageBox.Show("sorry,保存图片出现异常");
                        }
                    }
                }
            }
        }

    }
}