8

重複の可能性:
C#でWinformsコントロール/フォームのスクリーンショットを撮るにはどうすればよいですか?

名前と写真のリストが記載されたウィンドウフォームがあります。リストが長いので、そのためのスクロールパネルがあります。さて、このフォームを印刷したいのですが、下にスクロールすると見えない部分が見えるので、印刷機能は「見える」部分しか印刷しないので、印刷できません。では、フォーム全体を一度に印刷する方法はありますか?

4

2 に答える 2

3

VisualBasicPowerPacksツールボックスで印刷フォームコントロールを探します

スクロール可能なフォームの完全なクライアント領域を印刷するには、これを試してください...

1.ツールボックスで、[Visual Basic PowerPacks]タブをクリックし、PrintFormコンポーネントをフォームにドラッグします。

PrintFormコンポーネントがコンポーネントトレイに追加されます。

2. [プロパティ]ウィンドウで、PrintActionプロパティをPrintToPrinterに設定します。

3.適切なイベントハンドラー(たとえば、印刷ボタンのClickイベントハンドラー)に次のコードを追加します。

1.PrintForm1.Print(Me、PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

これを試してみて、どのように機能するか教えてください。

于 2012-12-31T18:17:31.410 に答える
2

これは完全な答えではありませんが、フォーム上のスクロール可能なPanelコントロールのスクリーンショット(ビットマップ)を取得するコードの一部です。大きな欠点は、スクリーンショットの撮影中に画面がちらつくことです。単純なアプリでテストしたので、すべての場合に機能するとは限りませんが、それが出発点になる可能性があります。

使用方法は次のとおりです。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent(); // create a scrollable panel1 component
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TakeScreenshot(panel1, "C:\\mypanel.bmp");
    }
}

そしてここにユーティリティがあります:

    public static void TakeScreenshot(Panel panel, string filePath)
    {
        if (panel == null)
            throw new ArgumentNullException("panel");

        if (filePath == null)
            throw new ArgumentNullException("filePath");

        // get parent form (may not be a direct parent)
        Form form = panel.FindForm();
        if (form == null)
            throw new ArgumentException(null, "panel");

        // remember form position
        int w = form.Width;
        int h = form.Height;
        int l = form.Left;
        int t = form.Top;

        // get panel virtual size
        Rectangle display = panel.DisplayRectangle;

        // get panel position relative to parent form
        Point panelLocation = panel.PointToScreen(panel.Location);
        Size panelPosition = new Size(panelLocation.X - form.Location.X, panelLocation.Y - form.Location.Y);

        // resize form and move it outside the screen
        int neededWidth = panelPosition.Width + display.Width;
        int neededHeight = panelPosition.Height + display.Height;
        form.SetBounds(0, -neededHeight, neededWidth, neededHeight, BoundsSpecified.All);

        // resize panel (useless if panel has a dock)
        int pw = panel.Width;
        int ph = panel.Height;
        panel.SetBounds(0, 0, display.Width, display.Height, BoundsSpecified.Size);

        // render the panel on a bitmap
        try
        {
            Bitmap bmp = new Bitmap(display.Width, display.Height);
            panel.DrawToBitmap(bmp, display);
            bmp.Save(filePath);
        }
        finally
        {
            // restore
            panel.SetBounds(0, 0, pw, ph, BoundsSpecified.Size);
            form.SetBounds(l, t, w, h, BoundsSpecified.All);
        }
    }
于 2013-01-02T09:54:53.893 に答える