Ejemplo n.º 1
0
        private void ConvertStream(object input)
        {
            var convert = new WMFConversion.WMF2WPF();
            var outStream = new MemoryStream();
            var dpiX = 96;
            var dpiY = 96;

            try
            {
                var WPFCanvas = convert.Convert((Stream)input, dpiX, dpiY);

                if (WPFCanvas.Width == 0.0)
                    WPFCanvas.Width = 1000;

                if (WPFCanvas.Height == 0.0)
                    WPFCanvas.Height = 1000;

                var renderBitmap =
                  new RenderTargetBitmap(
                    (int)WPFCanvas.Width,
                    (int)WPFCanvas.Height,
                    96d,
                    96d,
                    PixelFormats.Pbgra32);
                renderBitmap.Render(WPFCanvas);

                // Use png encoder for our data
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                encoder.Save(outStream);
                outStream.Position = 0;

                Image img = new Bitmap(outStream);

                this._document = Document.FromImage(img);
                ((Layer)this._document.Layers[0]).IsBackground = false;
                ((Layer)this._document.Layers[0]).Name = "Metafile";
                this._document.Layers.Insert(0, Layer.CreateBackgroundLayer(img.Width, img.Height));
            }
            catch
            {
            }
        }
Ejemplo n.º 2
0
        private async void rootGrid_PointerPressed_1(object sender, PointerRoutedEventArgs e)
        {
            var picker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.List
            };
            picker.FileTypeFilter.Add(".WMF");

            var file = await picker.PickSingleFileAsync();
            if (file == null)
            {
                return;
            }

            var wmf = new WMFConversion.WMF2WPF();
            var image = wmf.Convert(await file.OpenStreamForReadAsync());

            rootGrid.Children.Clear();
            rootGrid.Children.Add(image);

        }
Ejemplo n.º 3
0
        private static void ConvertWmfFile(string sourcePath, string filePath, string destPath, FormatType format)
        {
            System.Windows.Controls.Canvas WPFCanvas = null;
            var convert = new WMFConversion.WMF2WPF();
            var dpiX = 96;
            var dpiY = 96;
            string imageSavePath = null;

            try
            {
                Console.WriteLine("Converting: " + filePath);

                if (format == FormatType.Png)
                {
                    dpiX = 96;
                    dpiY = 96;
                }
                else if (format == FormatType.Xaml)
                {
                    // For xaml we need to save bitmaps to file
                    imageSavePath = Path.Combine(destPath, Path.GetFileNameWithoutExtension(filePath) + "_");
                }

                if (format != FormatType.PngNative)
                {
                    using (var loadStream = new FileStream(filePath, FileMode.Open))
                    {
                        WPFCanvas = convert.Convert(loadStream, dpiX, dpiY, imageSavePath);
                    }
                }

                if (format == FormatType.Png)
                {
                    Directory.CreateDirectory(Path.Combine(destPath,
                                                           Path.GetDirectoryName(
                                                                   filePath.Substring(sourcePath.Length + 1)))
                                                                   );
                    var outStream = new FileStream(Path.Combine(destPath, Path.Combine(Path.GetDirectoryName(filePath.Substring(sourcePath.Length + 1)), Path.GetFileNameWithoutExtension(filePath) + ".png")), FileMode.Create);

                    var renderBitmap =
                      new RenderTargetBitmap(
                        (int)WPFCanvas.Width,
                        (int)WPFCanvas.Height,
                        96d,
                        96d,
                        PixelFormats.Pbgra32);
                    renderBitmap.Render(WPFCanvas);

                    // Use png encoder for our data
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    encoder.Save(outStream);
                    outStream.Close();
                    outStream.Dispose();
                }
                else if (format == FormatType.PngNative)
                {
                    Directory.CreateDirectory(Path.Combine(destPath,
                                                              Path.GetDirectoryName(
                                                                      filePath.Substring(sourcePath.Length + 1)))
                                                                      );
                    var outStream = new FileStream(Path.Combine(destPath, Path.Combine(Path.GetDirectoryName(filePath.Substring(sourcePath.Length + 1)), Path.GetFileNameWithoutExtension(filePath) + "_native.png")), FileMode.Create);

                    var metafile1 = new System.Drawing.Imaging.Metafile(filePath);
                    metafile1.Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
                    outStream.Close();
                    outStream.Dispose();
                }
                else
                {
                    File.WriteAllText(Path.Combine(destPath, Path.GetFileNameWithoutExtension(filePath) + ".xaml"), System.Windows.Markup.XamlWriter.Save(WPFCanvas));
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }