コード例 #1
0
        public static void GenerateImage(System.Windows.Controls.Viewbox control, Stream result)
        {
            //Set background to white
            //control.Background = System.Windows.Media.Brushes.White;

            System.Windows.Size controlSize = RetrieveDesiredSize(control);
            System.Windows.Rect rect        = new System.Windows.Rect(0, 0, controlSize.Width, controlSize.Height);

            System.Windows.Media.Imaging.RenderTargetBitmap rtb = new System.Windows.Media.Imaging.RenderTargetBitmap((int)controlSize.Width, (int)controlSize.Height, IMAGE_DPI, IMAGE_DPI, PixelFormats.Pbgra32);

            control.Arrange(rect);
            rtb.Render(control);

            System.Windows.Media.Imaging.PngBitmapEncoder png = new System.Windows.Media.Imaging.PngBitmapEncoder();
            png.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
            png.Save(result);
        }