SVG exported to EMF loses precision, but exported as PNG looks fine

1

0

I am Windows desktop programmer that needs to convert certain SVG into EMF in order to use it for programming.

My graphics design skills are at beginner level, but I have managed to convert SVG file into EMF successfully using Inkscape. However, the result is not looking as the original one, it is less "precise" so to say. If I export the SVG as PNG the result is the same as the original file. Unfortunately I need vector format that Windows recognizes and that is only EMF ( I am using pure WinAPI and C++, hence this limitation... ).

To see exactly what I mean, link to original SVG, and EMF and PNG that I made is here. Just click on the Download:test.rar above the 5 yellow stars ( see picture below ).

enter image description here

The problem seems to be the dimensions of the EMF file ( it should be 90 x 120 ) as the picture is crystal clear when I resize my application to bigger dimensions.

I am asking your help to get the same graphic quality of the EMF file as the original SVG.

To further help you in your efforts to solve my problem, here is a small application that will display EMF in a window. Just keep the EMF in the same place the application is, and name the EMF as test.emf.

Thank you for your time and efforts. Best regards.

AlwaysLearningNewStuff

Posted 2014-08-03T11:15:32.067

Reputation: 63

1"to get the same graphic quality of the EMF file as the original EMF"? Aren't the originals SVGs... – martineau – 2014-08-03T15:05:15.333

@martineau: I meant "as the original SVG"... I have edited my post. Thank you for your correction. – AlwaysLearningNewStuff – 2014-08-03T16:22:59.080

The "precision" issue may be inherent in the EMF format (or an limitation of Inkscape's export_to_EMF functionality). You might be able to work around it by exporting the SVG files at a higher resolution (like double) and then scaling it back down when you place them using the WinAPI. – martineau – 2014-08-03T16:51:49.833

@martineau: The "precision" issue may be inherent in the EMF format -> this may well be the case because even online converters produced the same result. You might be able to work around it by exporting the SVG files at a higher resolution -> you mean if I want 100 x 100 image then I would need at least 200 x 200 SVG? – AlwaysLearningNewStuff – 2014-08-03T17:06:04.110

Yes, that's what I meant. You should be able to scale it back down to the original size when you place it using Windows' API. – martineau – 2014-08-04T01:36:08.073

@martineau: I have tried scaling it to 200 x 200 but had no success. Then I have tried to scale it up to 800 x 800 but again it failed. Finally I tried with 600 x 600 but again no luck... I went to Object->Transform and chose Scale from the dialog box in Inkscape. I even tried with Apply to each object separately option but it did not helped me either... Maybe I am doing something wrong, can you try to do what you suggest with the SVG I linked to in my post? Thank you. – AlwaysLearningNewStuff – 2014-08-04T11:21:16.100

What do you mean by "had no success" and "it failed"? After you do that with everything in the SVG file, export it again, and the try accessing it with WinAPI and C++ again? – martineau – 2014-08-04T13:24:05.913

@martineau: What do you mean by "had no success" and "it failed"? -> the quality of the picture remained the same. After you do that with everything in the SVG file, export it again, and the try accessing it with WinAPI and C++ again? After scaling up the image, I click Save to save the changes, then Save as and save it as EMF. The quality of the image remains unchanged. I have noticed the same when Explorer displays the image. Inkscape gave the warning that some details might be lost when closing the program and asked to save as SVG instead EMF. Perhaps last part is important? – AlwaysLearningNewStuff – 2014-08-04T13:34:22.100

It's quite possible that when you save the file as EMF it gets converted into an image -- EMF files can contain embedded raster images as well as vector graphics. For the highest quality and consistency, I suggest you try to figure out some way to use the original SVG graphics rather than converting them. – martineau – 2014-08-04T16:56:47.207

Only one difference between the provided SVG and EMF catched my eye: In the former you do not have a black outline of e.g. the white waves, but in the latter you have. This outline isn't scaled down, hence messing up your EML at small scales. As I'm not completely sure, we are talking about the same issue (some graphics in your question would be helpful), please check out my edited EMF file: http://www.speedyshare.com/zBS7q/test-mod.emf -- if that looks fine, perhaps you can find an Inkscape export option concerning the outline (I don't know Inkscape... I used CorelDRAW to produce this file).

– mpy – 2014-08-05T18:36:57.803

@mpy: I have checked your EMF and got same result. If you could please be patient I will code a small application that will help you with testing and will upload it. I will edit my post with the link for this application and notify you. Thank for trying to help, I appreciate it! – AlwaysLearningNewStuff – 2014-08-05T18:45:40.867

Wouldn't it be easier just to post two images (desired result and result from your code? – mpy – 2014-08-05T18:51:33.953

@mpy: Done! See my edited post for the link. Don't forget to follow simple instructions so everything can work properly. – AlwaysLearningNewStuff – 2014-08-05T19:03:57.287

(Your link to the EMF viewer program is bad.) I agree that there is nothing wrong with your EMF. When viewed with Photoshop the EMF does not have an added frame, so this seems to be a problem with your viewer. – harrymc – 2014-08-05T20:36:45.380

@harrymc: Your link to the EMF viewer program is bad. I don't understand, have you clicked on Download:EMF app.rarjust above 5 yellow stars? Did you keep the EMF file and program at the same place e.g. in the same directory? When viewed with Photoshop the EMF does not have an added frame, so this seems to be a problem with your viewer I don't understand this part, but I believe you. Can you explain what you mean ( if it is not too broad of course )? Thank you. – AlwaysLearningNewStuff – 2014-08-05T20:42:38.870

1I think this was a momentary glitch with the sharing site, the link works fine now. – harrymc – 2014-08-05T20:51:06.583

@harrymc: OK, no problem. It seems that answer below sheds some useful light. I am using native GDI and it probably does use simple scaling algorithm. I just don't understand why do I need resampling algorithm when EMF is vector format... – AlwaysLearningNewStuff – 2014-08-05T20:54:21.993

Answers

2

I'll make a comparison with your test graphic:

  1. This is the PNG file you provide, so this probably is the desired result:

    enter image description here

  2. The output of your code, approx. at the same size:

    enter image description here

  3. The EMF file you provide, displayed in IrfanView (again approx. the same size)

    enter image description here

  4. Same as 3., but with View -> Display Options -> Use Resample turned on:

    enter image description here

Conclusion

As 4. look similar to 1. (i.e. the desired result), my conclusion is that the conversion to EMF format is perfectly fine, but your code scales the graphic with a simple, probably pixel based algorithm. So IMHO you need to implement e.g. a bilinear interpolation routine.

mpy

Posted 2014-08-03T11:15:32.067

Reputation: 20 866

I have upvoted your answer, as it seems to me that you have found the problem and a solution. Still, I am little dumbstruck, shouldn't EMF be vector format, meaning not losing quality during rescale? – AlwaysLearningNewStuff – 2014-08-05T20:38:00.900

IrfanView does a very poor job of displaying EMF. I have also tried it and it does horrible things. I think that it converts the EMF to a small bitmap, then displays it scaled up, which causes pixelation. Image resample applies only to bitmap images, never to vector graphics which should display correctly at any size. – harrymc – 2014-08-05T20:48:39.277

@harrymc: I agree with your remarks but my app does the same ( run it and check ). this is very odd behavior indeed. I am really dumbstruck and will post a question about this on StackOverflow just to see what's wrong. Meanwhile, I ask you to try and convert SVG file into EMF with Adobe Illustrator or Photoshop, just to see how my app will behave. It will give me useful info for guys at StackOverflow. Thank you. – AlwaysLearningNewStuff – 2014-08-05T21:00:11.903

Can you try the same? To convert SVG into EMF with Adobe Illustrator or something else, and test it with my app? Thank you. – AlwaysLearningNewStuff – 2014-08-05T21:00:54.783

2

When viewed with Photoshop, the EMF does not have an added frame, so this seems to be a problem with the viewer.

Not able to download your viewer for some obscure reason, I have used the Free WMF Viewer to view your EMF image,

The EMF file looks perfect - it scales up and down perfectly as should a vector graphics image. But that viewer also shows a black frame around the image.

I have also viewed your EMF using an old wmf/emf viewer that I have once written, which also doesn't show the black frame. I remark that in my own viewer I have used the bounding box only to determine the aspect ratio of the image (height vs. width), so as to display it correctly, meaning not squashed or stretched.

My conclusion is that both viewers display the bounding box as a frame. This is incorrect, since its purpose is to give information about the image's recommended viewing rectangle, rather than be displayed. Your process of generating the EMF seems therefore to be correct.

harrymc

Posted 2014-08-03T11:15:32.067

Reputation: 306 093

I have upvoted your post because I have found documented problems for EMF on Inkscape's site ( I can't remember the link right now ). They basically say that SVG must be "simple" ( solid fills simple strokes and stuff like that ) otherwise there will be problems. As I have asked you in my comment to member mpy's answer, can you convert the SVG I uploaded into EMF with illustrator and check it with my app? Thank you. – AlwaysLearningNewStuff – 2014-08-05T21:06:03.180

Illustrator can display the SVG, but my version doesn't know how to save in EMF format. I note that another image painter I used asked about the size before displaying the SVG, whether to display according to a given size or according to the size of the bounding box (meaning the viewing rectangle). I think that this bounding box in the SVG is the one that is badly translated by Inkscape to EMF. – harrymc – 2014-08-05T21:18:07.150

I appreciate your efforts. SVG was created with Inkscape by converting Corel Draw file. Perhaps if I use different tool to create SVG mistakes will disappear. In my main application I display EMF exported from another program ( ArcGis ) and everything works fine. I just hope that this is some bug with Inkscape and not larger issue I must solve. Thank you, we shall "talk" again soon. – AlwaysLearningNewStuff – 2014-08-05T21:22:50.170

I have tried an old wmf/emf viewer that I have once written, and it also doesn't show the black frame. I remark that I have used there the bounding box only to determine the aspect ratio of the image (height vs. width) so the image is displayed correctly. I still think that the viewers that display it interpret that box incorrectly, since its purpose is not to be displayed. – harrymc – 2014-08-07T09:20:00.587

I have downloaded Adobe Illustrator ( trial version ) and exported original EPS file inot EMF. The result remained the same. Asking for help at StackOverflow, I got an answer that manipulated the file before showing it. This means that the problem is in the way my framework outputs the file. Therefore I have decided to accept the other answer ( I have upvoted yours earlier ). Thank you for your time and help, I highly appreciate it. – AlwaysLearningNewStuff – 2014-08-17T18:53:32.640