3

テキスト ボックスをキャプチャして PDF 形式に変換したいと考えています。
次のコードを試しましたが、画面全体がキャプチャされます。テキストボックスの値だけをキャプチャするにはどうすればよいですか?

  1. コード:

    try
    {
        Rectangle bounds = this.Bounds;
        using (var bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty,
                                 bounds.Size);
            }
            bitmap.Save("C://Rectangle.bmp", ImageFormat.Bmp);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message.ToString());
    }
    
  2. PDFをエクスポートする場合:

    captureScreen();
    var doc = new PdfDocument();
    var oPage = new PdfPage();
    doc.Pages.Add(oPage);
    oPage.Rotate = 90;
    XGraphics xgr = XGraphics.FromPdfPage(oPage);
    XImage img = XImage.FromFile(@"C://Rectangle.bmp");
    xgr.DrawImage(img, 0, 0);
    doc.Save("C://RectangleDocument.pdf");
    doc.Close();
    
4

2 に答える 2

1

使用するべきではありませんthis.BoundsyourTextBox.Bounds

于 2012-10-12T11:57:08.723 に答える
1

正確な解決策ではありませんが、正しい方向へのヒントがいくつかあります。次のコードは、指定された四角形で特定のダイアログのスクリーンショットを取得します。クライアント領域のスクリーンショットからテキストボックスを抽出するように変更できます (タイトルバーのないダイアログなど)。

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWINFO
{
    public UInt32 cbSize;
    public RECT rcWindow;
    public RECT rcClient;
    public UInt32 dwStyle;
    public UInt32 dwExStyle;
    public UInt32 dwWindowStatus;
    public UInt32 cxWindowBorders;
    public UInt32 cyWindowBorders;
    public UInt16 atomWindowType;
    public UInt16 wCreatorVersion;
}

P/I によるちょっとしたネイティブ マジック:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO windowInfo);

[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

ダイアログのスクリーンショット方法。任意の Windows フォームのハンドル プロパティで hwnd を取得します。

protected Bitmap Capture(IntPtr hwnd, Rectangle rect)
{
    WINDOWINFO winInf = new WINDOWINFO();
    winInf.cbSize = (uint)Marshal.SizeOf(winInf);
    bool succ = PINativeOperator.GetWindowInfo(hwnd, ref winInf);

    if (!succ)
        return null;

    int width = winInf.rcClient.right - winInf.rcClient.left;
    int height = winInf.rcClient.bottom - winInf.rcClient.top;

    if (width == 0 || height == 0)
        return null;

    Graphics g = Graphics.FromHwnd(hwnd);
    IntPtr hdc = g.GetHdc();

    if(rect == Rectangle.Empty) {
        rect = new Rectangle(0, 0, width, height);
    }
    Bitmap bmp = new Bitmap(rect.Width, rect.Height);
    Graphics bmpG = Graphics.FromImage(bmp);
    PINativeOperator.BitBlt(bmpG.GetHdc(), 0, 0, rect.Width, rect.Height, hdc, rect.X, rect.Y, TernaryRasterOperations.SRCCOPY);
    bmpG.ReleaseHdc();

    return bmp;
}
于 2012-10-12T12:02:59.767 に答える