How do I resize (shrink) an EMF (Metafile) in .Net

2019-03-28 19:49发布

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

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

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

I haven't found a clear example in .Net (or any language platform for the matter) that deals with emf/metafiles.

I've looked in the Graphics Programming with GDI+ but it only introduces Metafiles.

I've tried Image Magick but you have to convert to another format (which I need to do anyway) and the result is blurry (when shrunk and converted to png for example).

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

Also, (don't laugh) I've opened it up in Window's Paint (one of the few image editing software programs that will open emf's) and resized the drawing, again it's blurry.

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

This works, but the resulting image is blurry. The code is just a generic image re-sizing routine, not specific to EMF's

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

3条回答
老娘就宠你
2楼-- · 2019-03-28 20:28

Im using the following code (similar to what you have after the edit) for re-sizing an emf image. It does not seem to get blurry.

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);
}
查看更多
成全新的幸福
3楼-- · 2019-03-28 20:40

.Net can load and draw EMF:

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

If the result image is fuzzy, consider save as PNG/GIF which has lossless compression, or render to a larger image.

If the result is still not good enough, then you need to use GDI32 to load and render.

查看更多
姐就是有狂的资本
4楼-- · 2019-03-28 20:40

EMF resizing can be quite tricky. You can rasterize the image in a smaller bitmap, but the results are not always what you'd expect. Here are the results of a view strategies:

3 different strategies

For my own project I decided to use the last strategy: blowing up the EMF in a bitmap 4x the size of the EMF, saving it to PNG and then saving the PNG in a smaller file.

I've written a blog on the subject: Rasterizing EMF files with .Net / C#. And I've created a class on GitHub that helps resizing EMF's, but it is a slightly more complicated way of doing things (with a bounding box).

查看更多
登录 后发表回答