【问题标题】:how to print a screen capture in wpf如何在 wpf 中打印屏幕截图
【发布时间】:2016-02-01 00:46:27
【问题描述】:

首先我不会说流利的英语。 反正。 我正在尝试这样做。 然而它不是第三天。 我现在正在做的是屏幕捕获后的程序屏幕打印。 我参考了这段代码。 https://social.msdn.microsoft.com/Forums/windows/en-US/0623964c-4bb4-44c0-a1cb-4dbb2fa161f0/need-simple-c-code-to-print-a-screen-capture?forum=winforms

但这仅适用于 winform。 我试图从 wpf 中尝试相同的方法。 我想自动适应页面。 这是我的代码

    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);
    }

【问题讨论】:

    标签: wpf printing screen capture


    【解决方案1】:

    您可以做的是使用 System.Drawing 即 Winforms 方式将屏幕截图捕获为位图,然后将捕获的位图转换为 BitmapSource 图像。

    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;
    

    屏幕被转换为 BitmapSource 后,您打印的内容相同。

    图像控制 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");
                }
    

    【讨论】:

    • 我有一个问题。 imageCapture.Arrange(new Rect(new Point(0, 0), imgControlWpf.DesiredSize));什么是“imagControlWpf”?
    • @Juyoung 抱歉,它应该是“imageCapture”。编辑了帖子!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2014-08-19
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    相关资源
    最近更新 更多