InkCanvas 保存图片

来源:互联网 发布:梦幻西游mac补丁 编辑:程序博客网 时间:2024/05/01 22:26

将  InkCanvas 里面的画面,保存为图片

* 该方法,保存的图片没有黑色的边框

 

private void ImageSave(InkCanvas inkCanvas, string _imageFile)
        {
            double width = inkCanvas.ActualWidth;
            double height = inkCanvas.ActualHeight;
            RenderTargetBitmap bmpCopied = new RenderTargetBitmap((int)Math.Round(width), (int)Math.Round(height), 96, 96, PixelFormats.Default);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext dc = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(inkCanvas);
                dc.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), new System.Windows.Size(width, height)));
            }
            bmpCopied.Render(dv);
            using (FileStream file = new FileStream(_imageFile,FileMode.Create, FileAccess.Write))
            {
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmpCopied));
                encoder.Save(file);
            }
        }