2012-12-31 36 views
7

可能性の重複:
How can I take a screenshot of a Winforms control/form in C#?スクロール可能なウィンドウフォームを印刷します。

私は名前や写真のリストを使用してWindowsフォームを持っています。リストは長いので、そのためのスクロールパネルがあります。さて、私はこのフォームを印刷したいと思いますが、印刷機能は下にスクロールすると見えない部分が見えるので、「表示」部分だけが印刷されるため、できません。ですから、フォーム全体を一度に印刷する方法はありますか?

+3

それは正確なだまされやすい人ではないのですが、問題は基本的に同じです。ここには簡単な解決策はありません。印刷したいものは実際には存在しません(描画されていないため)。印刷する前に、フォームをスクロールして複数のイメージをキャプチャする必要があります。フォームを正確に照合することについてあまり心配することなくデータを印刷する方が簡単かもしれません。 –

+1

@JonB - まあ、このような質問は、この繰り返し問題に対する一般的な答えを見つけるのに役立ちます。可能な複製は本当にうまく適用されないようです。それはこれでこれまでの唯一の答えは "それはできません"ですか? –

答えて

3

これを試してスクロール可能なフォームの完全なクライアント領域を印刷するにはVisual Basicのパワーパックツールボックス

で印刷フォームコントロールを探し...

ツールボックス1.In、Visual Basicのパワーパックをクリックしてくださいタブをクリックし、PrintFormコンポーネントをフォーム上にドラッグします。

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

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

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

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

この打撃を与えると私はそれがあなたのためにどのように動作するかを知ってみましょう。

+0

これは私のフォームの高さを764以上に伸ばすことができないことを識別するのが本当にうんざりです。あなたのソリューションを試しました。大きなフォームでは機能しません。スクロールでフォームのためにこれを行う他の方法がありますか?ありがとう – Brune

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); 
     } 
    } 
関連する問題