9

I have a listview control that is on a winforms form. It fills the full screen but there are more items there than the screen can show.

How can I take a screenshot of the whole control as if I could display the whole contents of the listview on screen? So if the whole listview takes 1000 x 4000 pixels, then I want an image/bitmap of that size.

How do I do this? When I try printscreen, it only returns what's on the screen and anything outside the screen appears grey.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

2 Answers2

10

Forms are controls, so you should be able to save the entire contents to a bitmap with something like:

var bm = new Bitmap(yourForm.Width, yourForm.Height);
yourForm.DrawToBitmap(bm, bm.Size);
bm.Save(@"c:\whatever.gif", ImageFormat.Gif);

Update

DrawToBitmap only draws what's on-screen. If you want to draw the entire contents of the list you must iterate through the list to find the size of the contents then draw each item. Something like:

var f = yourControl.Font;
var lineHeight = f.GetHeight();

// Find size of canvas
var s = new SizeF();
using (var g = yourControl.CreateGraphics())
{
    foreach (var item in yourListBox.Items)
    {
        s.Height += lineHeight ;
        var itemWidth = g.MeasureString(item.Text, f).Width;
        if (s.Width < itemWidth)
            s.Width = itemWidth;
    }

    if (s.Width < yourControl.Width)
         s.Width = yourControl.Width;
}

using( var canvas = new Bitmap(s) )
using( var g = Graphics.FromImage(canvas) )
{
    var pt = new PointF();
    foreach (var item in yourListBox.Items)
    {
        pt.Y += lineHeight ;
        g.DrawString(item.Text, f, Brushes.Black, pt);
    }

    canvas.Save(wherever);
}
Adam
  • 2,762
  • 1
  • 30
  • 32
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • Thanks, I tried it but the bitmap comes out as blue. I tried it on the listview not the form because the form is fixed, but the listview has a scrollbar. – Joan Venge Sep 29 '09 at 17:19
  • DrawToBitmap draws what's on screen. Are you trying to print the content of the ListView without the scrollbar? You will have to draw each item separately. – Dour High Arch Sep 29 '09 at 17:35
  • I see, now I am trying to print the whole thing, as if you had an infinitely large monitor. I don't care if the bitmap includes scrollbars or anything else, as long as it shows the whole control as a bitmap. – Joan Venge Sep 29 '09 at 21:01
1

unless you don't like bit blast,

Private Declare Function BitBlt Lib "gdi32" _
    (ByVal hDCDest As IntPtr, ByVal XDest As IntPtr, _
    ByVal YDest As IntPtr, ByVal nWidth As IntPtr, _
    ByVal nHeight As IntPtr, ByVal hDCSrc As IntPtr, _
    ByVal XSrc As IntPtr, ByVal YSrc As IntPtr, _
    ByVal dwRop As IntPtr) As IntPtr

Private Declare Function GetWindowDC Lib "user32" _
  (ByVal hWnd As IntPtr) As IntPtr

Private Declare Function ReleaseDC Lib "user32" _
  (ByVal hWnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Sub Blast()
    Dim dc As IntPtr
    dc = GetWindowDC(Me.Handle)
    Dim bm As Bitmap = New Bitmap(Me.Width, Me.Height)
    Dim g As Graphics = Graphics.FromImage(bm)
    Const vbSrcCopy = &HCC0020
    Dim gdc = g.GetHdc()
    BitBlt(gdc, 0, 0, Me.Width, Me.Height, dc, 0, 0, vbSrcCopy)
    g.ReleaseHdc()
    bm.Save("C:\yourfile.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
    ReleaseDC(Me.Handle, dc)
End Sub
Wolf-Kun
  • 679
  • 6
  • 12