将特定区域的图片作为屏幕截图保存到所需路径

saving a picture of a specific area taken as a screenshot to the desired path

用我给的代码,我可以截图canvas所在的部分。但是,我能够走上预定的道路。我想做的是通过 savefiledialog 将图像保存到我想要的部分。我该怎么做。

 private void btnKaydet_Click(object sender, RoutedEventArgs e)
    {
        UIElement element = cnvs as UIElement;
        Uri path = new Uri(@"c:\screenshot.png");
        CaptureScreen(element, path);   
    }

public void CaptureScreen(UIElement source, Uri destination)
    {
        try
        {
            double Height, renderHeight, Width, renderWidth;

            Height = renderHeight = source.RenderSize.Height;
            Width = renderWidth = source.RenderSize.Width;

            //Specification for target bitmap like width/height pixel etc.
            RenderTargetBitmap renderTarget = new
            RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96,
            PixelFormats.Pbgra32);
            //creates Visual Brush of UIElement
            VisualBrush visualBrush = new VisualBrush(source);

            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext =
            drawingVisual.RenderOpen())
            {
                //draws image of element
                drawingContext.DrawRectangle(visualBrush, null, new
                Rect(new System.Windows.Point(0, 0), new System.Windows.Point(Width, Height)));
            }
            //renders image
            renderTarget.Render(drawingVisual);

            //PNG encoder for creating PNG file
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderTarget));
            using (FileStream stream = new
            FileStream(destination.LocalPath, FileMode.Create,
            FileAccess.Write))
            {
                encoder.Save(stream);       
            }

            
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

我们可以使用FileSaveDialogclass.

在你的using语句处

using Microsoft.Win32;
using System.Windows;

您可能需要添加对 System.Windows.Forms

的引用

如果您选择不使用 Microsoft 版本,也可以在此处找到流行的 WPF 开源对话框库。 https://github.com/ookii-dialogs/ookii-dialogs-wpf

  private void BtnKaydet_OnClick(object sender, RoutedEventArgs e)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        if (saveFileDialog.ShowDialog() == true)
        {
            UIElement element = this.cnvs as UIElement;
            Uri path = new Uri(saveFileDialog.FileName);
            CaptureScreen(element, path);
        }
    }
 public void CaptureScreen(UIElement source, Uri destination)
    {
        try
        {
            double Height, renderHeight, Width, renderWidth;
            Height = renderHeight = source.RenderSize.Height;
            Width = renderWidth = source.RenderSize.Width;

            //Specification for target bitmap like width/height pixel etc.
            RenderTargetBitmap renderTarget =
                new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
            //creates Visual Brush of UIElement
            VisualBrush visualBrush = new VisualBrush(source);
            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {
                //draws image of element
                drawingContext.DrawRectangle(visualBrush, null,
                    new Rect(new System.Windows.Point(0, 0), new System.Windows.Point(Width, Height)));
            }

            //renders image
            renderTarget.Render(drawingVisual);

            //PNG encoder for creating PNG file
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(renderTarget));
            using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
            {
                encoder.Save(stream);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

您还可以使用过滤器参数指定允许的文件扩展名类型(.png、.jpg 等)。 https://wpf-tutorial.com/dialogs/the-savefiledialog/