Ejemplo n.º 1
0
        private void Export(device target)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.DefaultExt = ".png";
            dlg.Filter     = "PNG files (.png)|*.png";

            Nullable <bool> result = dlg.ShowDialog();

            if (result == true)
            {
                UserControl export;

                if (target == device.stick)
                {
                    export = new Stick(null);
                }
                else if (target == device.stickHornet)
                {
                    export = new StickHornet(null);
                }
                else
                {
                    export = new Throttle(null);
                }

                try
                {
                    Size pageSize = new Size(453 * 10, 605 * 10);

                    ((IControlSet)export).SetSet(controls);
                    export.Measure(pageSize);
                    export.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));

                    RenderTargetBitmap rtb = new RenderTargetBitmap((int)export.ActualWidth, (int)export.ActualHeight, 96, 96, PixelFormats.Pbgra32);
                    DrawingVisual      dv  = new DrawingVisual();
                    using (DrawingContext ctx = dv.RenderOpen())
                    {
                        VisualBrush vb = new VisualBrush(export);
                        ctx.DrawRectangle(vb, null, new Rect(new Point(), pageSize));
                    }
                    rtb.Render(dv);

                    PngBitmapEncoder png = new PngBitmapEncoder();
                    png.Frames.Add(BitmapFrame.Create(rtb));

                    using (Stream fileStream = new FileStream(dlg.FileName, FileMode.Create))
                    {
                        png.Save(fileStream);
                    }
                }
                catch
                {
                    MessageBox.Show(this, "Error occured during chart export saving!", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
                finally
                {
                    ((IControlSet)export).CleanUp();
                }
            }
        }