Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display a emf image in WPF application.I have succeeded to use the following two methods convert it,but both of they are so slow.I want get a fast way? Who can help me!! thanks....

Method one :
C#
using (Stream stream = new MemoryStream(bytes))
                    {
                        stream.Position = 0;
                        Metafile mimg = new Metafile(stream);

                        using (Stream streamEx = new MemoryStream())
                        {
                            mimg.Save(streamEx, ImageFormat.Png);//Almost four seconds 
                            streamEx.Position = 0;

                            img = new BitmapImage();
                            img.BeginInit();
                            img.StreamSource = streamEx;
                            img.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
                            img.CacheOption = BitmapCacheOption.OnLoad;
                            img.EndInit();
                        }
                    }


Method two:
C#
using (Stream stream = new MemoryStream(bytes))
                   {
                       stream.Position = 0;
                       System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream);//Almost three seconds

                       myImg1.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                       bitmap.GetHbitmap(),
                                       IntPtr.Zero,
                                       new Int32Rect(0, 0, RcBill.Width, RcBill.Height),
                                       BitmapSizeOptions.FromEmptyOptions());
                       NativeMethods.DeleteObject(bitmap.GetHbitmap());
                   }
Posted
Updated 12-Apr-12 16:58pm
v2
Comments
TRK3 13-Apr-12 17:47pm    
How big is your EMF file? How big is the resulting bitmap? And what sort of machine are you running this on?

If the file and bitmap are huge and the machine is slow, then 3-4 seconds might not be unreasonable.

That said, I'd suggest trying the same thing in native C++ using the Win32 GDI stuff directly and see if their is a significant speed difference. I would hope that their isn't much speed difference, but I could imagine all sorts of ways that the .NET graphics functions might have been implemented that would make them significantly slower than the native versions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900