Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 632
Chris Weber Avatar asked Aug 23 '12 19:08

Chris Weber


1 Answers

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);
}
like image 73
Magnus Avatar answered Oct 05 '22 13:10

Magnus