none
WPF InkCanvas导出图片没内容,请教各位专家! RRS feed

  • 问题

  •  
    以下是代码,InkCanvas的背景是ImageBrush。

    RenderTargetBitmap rtb = new RenderTargetBitmap((int)Window.ActualWidth, (int)Window.ActualHeight, 1 / 100, 1 / 100, PixelFormats.Pbgra32);
                    for (int i = 0; i <InkCanvasList.Count; i++)
                    {
                        string picName = string.Format("{0}.JPG", i);
                        using (FileStream fs = new FileStream(picfile + "\\" + picName, FileMode.Create))
                        {
                            VisualBrush vbrush = new VisualBrush(InkCanvasList[i]);
                            DrawingVisual drawingVisual = new DrawingVisual();
                            DrawingContext drawingContext = drawingVisual.RenderOpen();
                            vbrush.Stretch = Stretch.Fill;
                            vbrush.TileMode = TileMode.Tile;
                            using (drawingContext)
                            {
                                drawingContext.PushTransform(new ScaleTransform(1, 1));
                                drawingContext.DrawRectangle(vbrush, null, new Rect(new System.Windows.Point(0, 0),
                                    new System.Windows.Point(Window.ActualWidth, Window.ActualHeight)));
                            }
                            rtb.Render(drawingVisual);

                            rtb.Render(InkCanvasList[i]); //呈现位图对象
                            JpegBitmapEncoder be = new JpegBitmapEncoder(); //指定格式 
                            be.QualityLevel = 90;
                            be.Frames.Add(BitmapFrame.Create(rtb));
                            be.Save(fs);
                            fs.Dispose();
                            fs.Close();
                        }
                    }

    2018年7月19日 9:41

全部回复

  • Hi Marry_qing,

    你能把你的xaml部分的代码提供一下吗?InkCanvasList是什么?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年7月20日 7:38
    版主
  • 參考一下:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                inkCanvas.UseCustomCursor = true;
                inkCanvas.Cursor = Cursors.Pen;
                SetDrawingAttributes();
            }
    
            private void SetDrawingAttributes()
            {
                inkCanvas.DefaultDrawingAttributes.Color = Colors.DarkBlue;
                inkCanvas.DefaultDrawingAttributes.FitToCurve = true;
                inkCanvas.DefaultDrawingAttributes.IgnorePressure = false;
                inkCanvas.DefaultDrawingAttributes.IsHighlighter = false;
                inkCanvas.DefaultDrawingAttributes.StylusTip = StylusTip.Ellipse;
                inkCanvas.DefaultDrawingAttributes.Width = 18;
                inkCanvas.DefaultDrawingAttributes.Height = 18;
            }
    
            private void SaveButton_Clicked(object sender, RoutedEventArgs e)
            {
                if (inkCanvas.Strokes.Count <= 0) return;
                var dialog = new SaveFileDialog
                {
                    Filter = "PNG files (*.png)|*.png",
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),
                };
              
                if (dialog.ShowDialog() == true)
                {
                    var width = (int)inkCanvas.ActualWidth;
                    var height = (int)inkCanvas.ActualHeight;
    
                    var bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
                    bitmap.Render(inkCanvas);
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(bitmap));
    
                    using (var stream = new FileStream(dialog.FileName, FileMode.Create))
                    {
                        encoder.Save(stream);
                    }
                }
            }
    
    
    
            private void ClearButton_Clicked(object sender, RoutedEventArgs e)
            {
                inkCanvas.Strokes.Clear();
            }
    
    
    }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/


    2018年7月24日 14:29
  • InkCanvasList是多个InkCanvas画板,有多页InkCanvas,我需要把多页保存为图片。
    2018年7月26日 1:56
  • 单个InkCanvas的可以的,我现在有多个InkCanvas,需要循环一个InkCanvas集合,把集合里的画板保存为图片。
    2018年7月26日 1:58