我在winforms表单上有一个listview
控件。它填满了整个屏幕,但屏幕上显示的项目比屏幕更多。
如何截取整个控件的屏幕截图,就好像我可以在屏幕上显示listview
的全部内容一样?因此,如果整个listview
需要1000 x 4000像素,那么我想要一个这样大小的图像/位图。
我该怎么做?当我尝试打印屏幕时,它只返回屏幕上的内容,屏幕外的任何内容都显示为灰色。
答案 0 :(得分:9)
表单是控件,因此您应该能够将整个内容保存到位图,例如:
var bm = new Bitmap(yourForm.Width, yourForm.Height);
yourForm.DrawToBitmap(bm, bm.Size);
bm.Save(@"c:\whatever.gif", ImageFormat.Gif);
DrawToBitmap
只能在屏幕上显示内容。如果要绘制列表的全部内容,则必须遍历列表以查找内容的大小,然后绘制每个项目。类似的东西:
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;
}
}
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);
}
答案 1 :(得分:1)
除非你不喜欢爆炸,否则
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