How to convert SVG file to EMF file in C #

You can definitely convert SVG to EMF, for example this site . I wonder if this conversion can be achieved in C #?


Update:

I tried reading the SVG file using SVG.NET and drawing it in Graphics , and then tried to export Image as a MetaFile in the .emf extension (I followed the instructions here: GDI + / C #: how to save the image as EMF? ). The read was successful, and the image was exported as .emf. However, when I opened this .emf in PowerPoint, it could not have been ungrouped, which indicated that the drawing information for this file was not actually reset correctly.

Update 2:

Now it exports ungroup-able.emf, but ungrouping shows a very bad result. I used the following code to create .emf:

 private void OpenPictureButtonClick(object sender, EventArgs e) { var openFileDialog = new OpenFileDialog(); openFileDialog.ShowDialog(); _svgDoc = SvgDocument.Open(openFileDialog.FileName); RenderSvg(_svgDoc); } private void SavePictureClick(object sender, EventArgs e) { var saveFileDialog = new SaveFileDialog {Filter = "Enhanced Meta File | *.Emf"}; saveFileDialog.ShowDialog(); var path = saveFileDialog.FileName; var graphics = CreateGraphics(); var img = new Metafile(path, graphics.GetHdc()); var ig = Graphics.FromImage(img); _svgDoc.Draw(ig); ig.Dispose(); img.Dispose(); graphics.ReleaseHdc(); graphics.Dispose(); } private void RenderSvg(SvgDocument svgDoc) { svgImageBox.Image = svgDoc.Draw(); } 
+5
source share
1 answer

I had the same problem but the search had no results.
Finally I ended up with my simple simple solution below. I used SVG.NET .

 public static byte[] ConvertToEmf(string svgImage) { string emfTempPath = Path.GetTempFileName(); try { var svg = SvgDocument.FromSvg<SvgDocument>(svgImage); using (Graphics bufferGraphics = Graphics.FromHwndInternal(IntPtr.Zero)) { using (var metafile = new Metafile(emfTempPath, bufferGraphics.GetHdc())) { using (Graphics graphics = Graphics.FromImage(metafile)) { svg.Draw(graphics); } } } return File.ReadAllBytes(emfTempPath); } finally { File.Delete(emfTempPath); } } 

First I create a temporary file. Then I use the Draw(Graphics) method to save emf in it. And finally, I read bytes from a temporary file.
Do not try to use MemoryStream for Metafile . Unfortunately, it does not work.

+6
source

Source: https://habr.com/ru/post/1203828/


All Articles