Metafile to Bitmap

I need to give selected text from Visio (it's give me metafile) and convert it to bitmap image. I use such code:

using (Metafile winMetafile = (System.Drawing.Imaging.Metafile)Image.FromFile(pathToMetaFile, true))
        {
            var header = winMetafile.GetMetafileHeader();
            var kw = header.Bounds.Width / header.DpiX;
            var kh = header.Bounds.Height / header.DpiY;

            var width = kw * Resolutions.ScreenDpi;
            var height = kh * Resolutions.ScreenDpi;

            using (var bitmap = new Bitmap((int)width, (int)height, winMetafile.PixelFormat))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(System.Drawing.Color.White);g.DrawImage(winMetafile, 0, 0, width, height);
                    this.ImagePreviewSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    this.ImageWidth = this.ImagePreviewSource.Width;
                    this.ImageHeight = this.ImagePreviewSource.Height;
                }
            }
        }

It's work fine in old versions of Visio, like 2010, 2008 etc. But when I try it in Visio 2015 - it's work, but with very low quality. Images in bitmap looks like "stair-stepping". But when I open metafile - it's look good. I guess that problem links to alpha channel or opacity, but I don't know how to fix it. Could anybody help me.

Update 1

I wrote wrong version of Visio. I mean that I use Visio 2013 (not 2015).



  • Edited by rubkaarchi Tuesday, January 13, 2015 7:47 AM
January 12th, 2015 3:23pm

Hi,

Based on your description, I actually do not know which program you are using. As far as I know, Microsoft Visio only has Visio 2007/2010/2013, not has the 2008 and 2015 edition, but Visual Studio has 2008/2010/2015.

If you are using Visio to do this, I recommend you post the question to windows desktop forum:
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/home?forum=windowsgeneraldevelopmentissues

If you are using Visual Studio, I recommend you post the question to C# forum: 

https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

Hope it's helpful.

Regards,

George Zhao
TechNet Community Support

Free Windows Admin Tool Kit Click here and download it now
January 13th, 2015 9:40am

Sorry, you are right. My code works fine in Visio 2010, 2007, but works with very low quality in Visio 2013.
January 13th, 2015 10:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics