如何在wpf中打印屏幕截图
首先,我英语说得不流利。 无论如何 我正试着这么做。 然而,这不是第三天。 我现在正在做的是程序截屏后的屏幕打印。 我指的是这个代码。 但这只适用于winform。 我试着从wpf那里得到同样的结果。 我想自动适应页面。 这是我的密码如何在wpf中打印屏幕截图,wpf,printing,screen,capture,Wpf,Printing,Screen,Capture,首先,我英语说得不流利。 无论如何 我正试着这么做。 然而,这不是第三天。 我现在正在做的是程序截屏后的屏幕打印。 我指的是这个代码。 但这只适用于winform。 我试着从wpf那里得到同样的结果。 我想自动适应页面。 这是我的密码 Bitmap bmpScreenshot; void bt1_MouseDown(object sender, MouseButtonEventArgs e) { var cv = sender as Canvas;
Bitmap bmpScreenshot;
void bt1_MouseDown(object sender, MouseButtonEventArgs e)
{
var cv = sender as Canvas;
var btName = cv.Name;
if (btName.Contains("8"))
{
MakeScreenshot();
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += printPage;
if (printDlg.ShowDialog() == true)
{
pd.Print();
}
}
}
public System.Windows.Point Location
{
get
{
return new System.Windows.Point(Left, Top);
}
set
{
Left = value.X;
Top = value.Y;
}
}
public void MakeScreenshot()
{
Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
FrameworkElement pnlClient = this.Content as FrameworkElement;
double dWidth = -1;
double dHeight = -1;
if (pnlClient != null)
{
dWidth = pnlClient.ActualWidth;
dHeight = pnlClient.ActualHeight;
}
var desktop = System.Windows.SystemParameters.WorkArea;
this.Left = desktop.Right - this.Width;
this.Top = desktop.Bottom - this.Height;
bmpScreenshot = new Bitmap((int)dWidth, (int)dHeight, g);
var memoryGrphics = Graphics.FromImage(bmpScreenshot);
memoryGrphics.CopyFromScreen((int)this.Location.X, (int)this.Location.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size);
}
bmpScreenshot.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
}
private void printPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(bmpScreenshot, 0, 0);
}
您可以使用System.Drawing(即Winforms方式)将屏幕截图捕获为位图,然后将捕获的位图转换为位图源图像
private static BitmapSource CopyScreen()
{
using (var screenBmp = new Bitmap(
(int)SystemParameters.PrimaryScreenWidth,
(int)SystemParameters.PrimaryScreenHeight,
PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
return Imaging.CreateBitmapSourceFromHBitmap(
screenBmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
}
您需要添加对System.Drawing和以下命名空间的引用:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
将屏幕转换为位图源后,可以打印相同的内容
图像控制xaml
<Image Name='imageCapture' Stretch='UniformToFill'/>
///Print Screen shot code
PrintDialog imgControlPrint = new PrintDialog();
///img Control wpf
imageCapture.Source=CopyScreen();
if ((bool)imgControlPrint.ShowDialog().GetValueOrDefault())
{
imageCapture.Measure(new Size(imgControlPrint.PrintableAreaWidth,imgControlPrint.PrintableAreaHeight));
imageCapture.Arrange(new Rect(new Point(0, 0), imageCapture.DesiredSize));
imgControlPrint.PrintVisual(imageCapture, "Screen Shot");
}
///打印屏幕截图代码
PrintDialog imgControlPrint=新建PrintDialog();
///img控制wpf
imageCapture.Source=CopyScreen();
if((bool)imgControlPrint.ShowDialog().GetValueOrDefault())
{
imageCapture.Measure(新尺寸(imgControlPrint.PrintableAreaWidth,imgControlPrint.PrintableAreaHeight));
imageCapture.Arrange(新的Rect(新的点(0,0),imageCapture.DesiredSize));
PrintVisual(图像捕获,“屏幕截图”);
}
您可以使用System.Drawing(即Winforms way)将屏幕截图捕获为位图,然后将捕获的位图转换为位图源图像
private static BitmapSource CopyScreen()
{
using (var screenBmp = new Bitmap(
(int)SystemParameters.PrimaryScreenWidth,
(int)SystemParameters.PrimaryScreenHeight,
PixelFormat.Format32bppArgb))
{
using (var bmpGraphics = Graphics.FromImage(screenBmp))
{
bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
return Imaging.CreateBitmapSourceFromHBitmap(
screenBmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
}
您需要添加对System.Drawing和以下命名空间的引用:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
将屏幕转换为位图源后,可以打印相同的内容
图像控制xaml
<Image Name='imageCapture' Stretch='UniformToFill'/>
///Print Screen shot code
PrintDialog imgControlPrint = new PrintDialog();
///img Control wpf
imageCapture.Source=CopyScreen();
if ((bool)imgControlPrint.ShowDialog().GetValueOrDefault())
{
imageCapture.Measure(new Size(imgControlPrint.PrintableAreaWidth,imgControlPrint.PrintableAreaHeight));
imageCapture.Arrange(new Rect(new Point(0, 0), imageCapture.DesiredSize));
imgControlPrint.PrintVisual(imageCapture, "Screen Shot");
}
///打印屏幕截图代码
PrintDialog imgControlPrint=新建PrintDialog();
///img控制wpf
imageCapture.Source=CopyScreen();
if((bool)imgControlPrint.ShowDialog().GetValueOrDefault())
{
imageCapture.Measure(新尺寸(imgControlPrint.PrintableAreaWidth,imgControlPrint.PrintableAreaHeight));
imageCapture.Arrange(新的Rect(新的点(0,0),imageCapture.DesiredSize));
PrintVisual(图像捕获,“屏幕截图”);
}
我有个问题。imageCapture.Arrange(新的Rect(新的点(0,0),imgControlWpf.DesiredSize));什么是“imagControlWpf”?@Juyoung srry它应该是“imageCapture”。编辑了这篇文章!我有个问题。imageCapture.Arrange(新的Rect(新的点(0,0),imgControlWpf.DesiredSize));什么是“imagControlWpf”?@Juyoung srry它应该是“imageCapture”。编辑了这篇文章!