【问题标题】:saving a picture of a specific area taken as a screenshot to the desired path将作为屏幕截图的特定区域的图片保存到所需路径
【发布时间】:2022-01-13 17:36:47
【问题描述】:

使用我提供的代码,我可以截取画布所在部分的屏幕截图。但是,我能够走上预定的道路。我想要做的是我想通过 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());
        }
    }

【问题讨论】:

    标签: c# wpf screenshot


    【解决方案1】:

    我们可以使用FileSaveDialog 类。

    在你的 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/

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多