zoukankan      html  css  js  c++  java
  • C# 实现图片类型的相互转换

    winform窗体:

     winform窗体后台代码:

    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 WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private System.Drawing.Bitmap MyBitmap;
            private void button1_Click(object sender, EventArgs e)
            {
                //打开图像文件
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "图像文件(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP图像文件(*.bmp)|*.bmp|Tiff图像文件(*.tif;*.tiff)|*.tif;*.tiff|Png图像文件(*.png)| *.png |所有文件(*.*)|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //得到原始大小的图像
                    Bitmap SrcBitmap = new Bitmap(openFileDialog.FileName);
                    //得到缩放后的图像
                    MyBitmap = new Bitmap(SrcBitmap);
                    this.pictureBox1.Image = MyBitmap;
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //转换图像文件
                if (MyBitmap == null)
                {
                    MessageBox.Show("请首先选择一幅图像!", "信息提示");
                    return;
                }
                SaveFileDialog saveDlg = new SaveFileDialog();
                if (saveDlg.ShowDialog() == DialogResult.Cancel)
                    return;
                string fileName = saveDlg.FileName;
                try
                {
                    if (this.comboBox1.SelectedIndex == 0)
                    {
                        MyBitmap.Save(fileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                    if (this.comboBox1.SelectedIndex == 1)
                    {
                        MyBitmap.Save(fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
                    }
                    if (this.comboBox1.SelectedIndex == 2)
                    {
                        MyBitmap.Save(fileName + ".png", System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    if (this.comboBox1.SelectedIndex == 3)
                    {
                        MyBitmap.Save(fileName + ".gif", System.Drawing.Imaging.ImageFormat.Png);
                    }
                    if (this.comboBox1.SelectedIndex == 4)
                    {
                        MyBitmap.Save(fileName + ".tif", System.Drawing.Imaging.ImageFormat.Tiff);
                    }
                    if (this.comboBox1.SelectedIndex == 5)
                    {
                        MyBitmap.Save(fileName + ".wmf", System.Drawing.Imaging.ImageFormat.Wmf);
                    }
                    if (this.comboBox1.SelectedIndex == 6)
                    {
                        MyBitmap.Save(fileName + ".emf", System.Drawing.Imaging.ImageFormat.Emf);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "信息提示");
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBox1.SelectedIndex = 0;
            }
        }
    }
    

      

  • 相关阅读:
    Jaxb2 实现JavaBean与xml互转
    标准输入与标准输出
    linux 一行一行的读取文件
    linux $* 和$@ if [ ](字符串比较)
    scala 学习(三)——Array和ArrayBuffer
    Shell编程(六)awk工具
    Shell编程(五)脚本语法
    Shell编程(四)Shell变量
    Shell编程(三)Shell特性
    Shell编程(一)概览
  • 原文地址:https://www.cnblogs.com/xiong950413/p/14282673.html
Copyright © 2011-2022 走看看