zoukankan      html  css  js  c++  java
  • 多种图像格式相互转换工具的开发(附源代码)

      在开发井口装置绘制软件过程中,需要将矢量图形进行有序摆放,由于多数采用WMF或EMF图元文件格式,结合WMF和EMF的发展情况,确定采用EMF格式。但开发过程中遇到一些WMF格式文件,需要处理成EMF格式。利用万能的网络进行搜索,发现试用软件都带有水印,存在种种限制,GitHub、Codeproject、CodeGuru中的MetaFile类也都没有合适的工具。

      通过一天的研究,可以充分利用C#本身的强大功能,开发一个小巧的图像格式转换工具(存在GDI+一般性错误,不影响使用)。本工具支持BMP、EMF、WMF、GIF、JPG、PNG、TIFF、ICO八种格式的相互转换,尤其针对WMF和EMF图元文件的转换,填补了网上缺少相关工具的实际情况。

      

     

      WMF转EMF如下:

         

     

     源代码如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.Drawing.Imaging;
     10 using System.IO;
     11 using System.Runtime.InteropServices;
     12 
     13 namespace imageConverter
     14 {
     15     public partial class Form1 : Form
     16     {
     17         string srcFile = "";
     18         string dstFile = "";
     19         int nIndex = 0;
     20         string strFormat;
     21         public Form1()
     22         {
     23             InitializeComponent();
     24         }
     25 
     26         // Load the images.
     27         private void Form1_Load(object sender, EventArgs e)
     28         {
     29             lblFileName.Text = System.IO.Directory.GetCurrentDirectory();
     30             cmbFormat.SelectedIndex = 0;
     31         }
     32 
     33         private void btnOpen_Click(object sender, EventArgs e)
     34         {
     35             openFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
     36             openFileDialog1.Filter = "所有文件|*.*|位图文件|*.Bmp|增强图元文件|*.Emf|图元文件|*.Wmf|Gif格式图像|*.Gif|Jpeg格式图像|*.Jpeg|Png格式图像|*.Png|Tiff格式图像|*.Tiff|图标文件|*.Ico";
     37             openFileDialog1.RestoreDirectory = true;
     38             openFileDialog1.FilterIndex = 1;
     39             if (openFileDialog1.ShowDialog() == DialogResult.OK)
     40             {
     41                 srcFile = openFileDialog1.FileName;
     42                 lblFileName.Text = srcFile;
     43                 picImage.Image = new Bitmap(srcFile);
     44             }
     45         }
     46 
     47         private void cmbFormat_SelectedIndexChanged(object sender, EventArgs e)
     48         {
     49             nIndex = cmbFormat.SelectedIndex;
     50             if (nIndex >= 0)
     51             {
     52                 btnConvert.Enabled = true;
     53                 strFormat = "." + cmbFormat.Text;
     54             }
     55             else
     56             {
     57                 btnConvert.Enabled = false;
     58                 strFormat = "";
     59             }
     60         }
     61         private static ImageFormat GetImageFormat(int nSel)
     62         {
     63             switch (nSel)
     64             {
     65                 case 0:
     66                     return ImageFormat.Bmp;
     67                 case 1:
     68                     return ImageFormat.Emf;
     69                 case 2:
     70                     return ImageFormat.Wmf;
     71                 case 3:
     72                     return ImageFormat.Gif;
     73                 case 4:
     74                     return ImageFormat.Jpeg;
     75                 case 5:
     76                     return ImageFormat.Png;
     77                 case 6:
     78                     return ImageFormat.Tiff;
     79                 case 7:
     80                     return ImageFormat.Icon;
     81                 default:
     82                     throw new NotImplementedException();
     83             }
     84         }
     85 
     86         private void btnConvert_Click(object sender, EventArgs e)
     87         {
     88             using (Image img = Image.FromFile(srcFile))
     89             {
     90                 System.IO.FileInfo fi = new System.IO.FileInfo(srcFile);
     91                 saveFileDialog1.InitialDirectory = fi.DirectoryName;
     92                 saveFileDialog1.Filter = "位图文件|*.Bmp|增强图元文件|*.Emf|图元文件|*.Wmf|Gif格式图像|*.Gif|Jpeg格式图像|*.Jpeg|Png格式图像|*.Png|Tiff格式图像|*.Tiff|图标文件|*.Ico|所有文件|*.*";
     93                 saveFileDialog1.RestoreDirectory = true;
     94                 saveFileDialog1.FilterIndex = nIndex + 1;
     95                 saveFileDialog1.FileName = System.IO.Path.GetFileNameWithoutExtension(fi.Name);
     96                 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
     97                 {
     98                     dstFile = saveFileDialog1.FileName;
     99                     lblDstFilename.Text = dstFile;
    100                     if ((GetImageFormat(nIndex) == ImageFormat.Emf) || (GetImageFormat(nIndex) == ImageFormat.Wmf))
    101                     {
    102                         var g = picMetafile.CreateGraphics();
    103                         var img0 = new Metafile(dstFile, g.GetHdc());
    104                         var ig = Graphics.FromImage(img0);
    105                         ig.DrawImage(img, 0, 0);
    106                         ig.Dispose(); img0.Dispose(); g.ReleaseHdc(); g.Dispose();
    107                     }
    108                     else
    109                     {
    110                         img.Save(dstFile, GetImageFormat(nIndex));
    111                     }
    112                     picMetafile.Image = Image.FromFile(dstFile);
    113                     img.Dispose();
    114                     saveFileDialog1.Dispose();
    115                 }
    116             }
    117         }
    118     }
    119 }

    下载链接:https://files.cnblogs.com/files/gqzxm/imageConverter.rar

    本文来自博客园,作者:智能油气田ing,转载请注明原文链接:https://www.cnblogs.com/gqzxm/p/15302643.html

  • 相关阅读:
    Sentinel Dashboard(基于1.8.1)流控规则持久化到Nacos——涉及部分Sentinel Dashboard源码改造
    测试平台MeterSphere源码入门
    Java:利用BigDecimal类巧妙处理Double类型精度丢失
    SpringBoot整合任务调度框架Quartz及持久化配置
    任务调度框架Quartz快速入门!
    Kafka超详细学习笔记【概念理解,安装配置】
    Windows环境下Zookeeper安装配置
    SpringData JPA利用Specification多条件查询
    SpringBoot事件监听机制及观察者模式/发布订阅模式
    详解Java中的IO输入输出流!
  • 原文地址:https://www.cnblogs.com/gqzxm/p/15302643.html
Copyright © 2011-2022 走看看