在開發井口裝置繪制軟件過程中,需要將矢量圖形進行有序擺放,由於多數采用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