多种图像格式相互转换工具的开发(附源代码)

摘要:
在井口装置绘图软件的开发过程中,需要对矢量图形进行排序。由于大多数都使用WMF或EMF元文件格式,再加上WMF和EMF的开发,因此决定使用EMF格式。通过一天的研究,我们可以充分利用C#本身的强大功能,开发一个紧凑的图像格式转换工具。该工具支持BMP、EMF、WMF、GIF、JPG、PNG、TIFF和ICO格式的相互转换,特别是WMF和EMF元文件的转换,填补了互联网上缺乏相关工具的实际情况。

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

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

  多种图像格式相互转换工具的开发(附源代码)第1张

 

  WMF转EMF如下:

     多种图像格式相互转换工具的开发(附源代码)第2张

 

 源代码如下:

  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

免责声明:文章转载自《多种图像格式相互转换工具的开发(附源代码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Python:爬取中国各市的疫情数据并存储到数据库PowerDesigner中CDM和PDM如何定义外键关系下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

配置nginx

aNULL:!MD5:!...

用python调用caffe时出错:AttributeError: 'module' object has no attribute 'bool_'

下面给出了一个解决方案,即重命名冲突的io文件:numpyと PyCaffe公司が io。年が 竞争す る よ で す$ pythonclassify。py--raw_scale255~/caffe/101_ObjectCategories/airaires/image_0001.jpg../result.npyTraceback:文件“classif.py...

如何下载Chrome离线版EXE安装文件和MSI版安装文件

对于Chrome的稳定版本(官方版本),您只需添加“?”在Chrome的“最终用户许可协议”页面上的链接之后?Standalone=1对于Beta版和开发版Chrome,只需记住以下地址:http://dl.google.com/chrome/install/{versionnumber}/crome_安装程序中的版本号。exe表示要下载的Chrome版本号...

等保2.0四级安全要求

平等保护2.0四级安全要求四级安全保护能力:应能够在统一的安全战略下,防止恶意攻击、严重自然灾害和来自国家一级、敌对组织和资源丰富的威胁源的其他严重危害造成的资源损害。它应该能够及时检测和监控攻击和安全事件,所有功能都可以快速恢复。以下粗体字段是平等保护的第4级和第3级之间的差异,应予以更多注意。...

PbootCMS后台增加轮播图自定义分组名称

我们知道,在PbootCMS后台的旋转木马图形模块中,当添加新的旋转木马图时,您不能自己选择组。相反,您可以自动创建组,例如组1、组2和组3。这显然对客户的体验不友好,而且您无法直观地知道在网页的哪个位置使用了旋转木马图。让我们分享一下如何启用PbootCMS后台来添加、删除和修改旋转木马图形组。...

webstorm关闭烦人的eslint语法检查

使用eslint语法检查后,我们发现JS代码中到处都是红线。通过右键菜单中的fixeslint problems选项,我们可以发现页面代码格式完全被eslint包装。只需关闭exlint语法检查。看不见,想不起来。反向关闭不会影响代码开发,但相反,它会影响代码开发。关闭eslint位置:文件--˃设置--˃语言和框架--˃CodeQualityTools--...