How to resize (reduce) EMF (metafile) in .Net?

I have an EMF file. I want to resize it smaller.

How to do it in .net (or with any tool) without getting a blurry image ?

The resulting modified image will be converted to another format (png / jpg / whatever), I can handle this (I think).

I did not find a clear example in .Net (or any language platform for this question) that deals with emf / metafiles.

I looked at graphical programming using GDI +, but it only enters metafiles.

I tried Image Magick, but you need to convert it to a different format (which I need to do anyway) and the result will be blurry (e.g. compressed and converted to png).

I tried Inkscape, but you can only import the EMF file, and Inkscape imports it upside down and out of proportion to an existing drawing.

Also, (don’t laugh) I opened it in Window Paint (one of the few image editing programs that will open emf) and resize the drawing, again blurred.

Update: Here is the code I'm using to resize.

This works, but the resulting image is blurry. Code is just a normal non-EMF image redefinition procedure

private static Image resizeImage(Image imgToResize, Size size) { int sourceWidth = imgToResize.Width; int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)size.Width / (float)sourceWidth); nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (Image)b; } 

Source: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

+6
source share
3 answers

Im using the following code (similar to what you have after editing) to reselect the emf image. It does not seem to blur.

 var size = new Size(1000, 1000); using(var source = new Metafile("c:\\temp\\Month_Calendar.emf")) using(var target = new Bitmap(size.Width, size.Height)) using(var g = Graphics.FromImage(target)) { g.DrawImage(source, 0, 0, size.Width, size.Height); target.Save("c:\\temp\\Month_Calendar.png", ImageFormat.Png); } 
+5
source

.Net can load and draw EMF:

 Metafile myMetafile = new Metafile("SampleMetafile.emf"); graphics.DrawImage(myMetafile, 100, 100); 

If the result image is not clear, consider saving as PNG / GIF, which has lossless compression, or rendering to a larger image.

If the result is still not good enough, you need to use GDI32 for loading and rendering.

+1
source

Resizing an EMF can be quite complicated. You can rasterize the image in a smaller bitmap, but the results are not always what you expect. Here are the results of your viewing strategies:

3 different strategies

For my own project, I decided to use the last strategy: explode EMF in 4x raster size EMF, save it in PNG and then save PNG in a smaller file.

I wrote a blog on this topic: Rasterizing EMF files with .Net / C # . And I created a class on GitHub that helps resize EMF, but it's a little harder than doing things (with a bounding box).

0
source

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


All Articles