【问题标题】:Printing only the active window in C#在 C# 中仅打印活动窗口
【发布时间】:2014-10-22 07:54:31
【问题描述】:

我只想打印来自 C# 应用程序的活动窗口。使用 CopyFromScreen() 不起作用,因为它会截取屏幕截图,并且该窗口可能会被其他出现的临时窗口部分隐藏。所以我尝试了 PrintWindow():

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.PageScale = 10.0f;
    Graphics g = e.Graphics;
    PrintWindow(this.Handle, g.GetHdc(), 0);
}

无论我使用哪种 PageScale,这都会生成一个小邮票大小的窗口打印输出。 有什么想法吗?

编辑 这就是诀窍:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics myGraphics = CreateGraphics();
    var bitmap = new Bitmap(Width, Height, myGraphics);
    DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
    e.Graphics.DrawImage(bitmap, 0, 0);
}

【问题讨论】:

    标签: c# printing screenshot scaling


    【解决方案1】:

    搜索了一下,我发现:Copying content from a hidden or clipped window in XP?

    看来您需要准备一个位图来存储页面:

    // Takes a snapshot of the window hwnd, stored in the memory device context hdcMem
    HDC hdc = GetWindowDC(hwnd);
    if (hdc)
    {
        HDC hdcMem = CreateCompatibleDC(hdc);
        if (hdcMem)
        {
            RECT rc;
            GetWindowRect(hwnd, &rc);
    
            HBITMAP hbitmap = CreateCompatibleBitmap(hdc, RECTWIDTH(rc), RECTHEIGHT(rc));
            if (hbitmap)
            {
                SelectObject(hdcMem, hbitmap);
    
                PrintWindow(hwnd, hdcMem, 0);
    
                DeleteObject(hbitmap);
            }
            DeleteObject(hdcMem);
        }
        ReleaseDC(hwnd, hdc);
    }
    

    【讨论】:

    • 可能不是C#,但是C代码表明你可能需要创建一个窗口大小的位图缓冲区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2012-08-19
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多