将作为屏幕截图的特定区域的图片保存到所需路径
Posted
技术标签:
【中文标题】将作为屏幕截图的特定区域的图片保存到所需路径【英文标题】: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());
【问题讨论】:
【参考方案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/
【讨论】:
以上是关于将作为屏幕截图的特定区域的图片保存到所需路径的主要内容,如果未能解决你的问题,请参考以下文章
App Store Connect 上的错误消息:无法保存主要区域设置 - 您必须为该语言的每个版本提供所需的屏幕截图
C#制作截图软件时,怎样不截取软件窗口本身? 看下图:软件自身也被截下来了