• 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;
            }
        }
    }
    

      

  • 相关阅读:
    FPGA时序约束的几种方法
    使用NiosII代替SignalTap来监测FPGA内部数据
    Modelsim的使用
    Modelsim+Debussy
    ChipScope用法总结
    QuartusII增量编译的个人学习
    quartus II .qsf文件(zz)
    RAM与Nand/Nor flash之间的区别 (转)
    黑金资料AX301_A的Quartus工程建立、编译及引脚分配、程序下载
    关于sg90舵机的,要知道!要注意!
  • 原文地址:https://www.cnblogs.com/xiong950413/p/14282673.html
Copyright © 2020-2023  润新知