我怎么可以把一个WinForms控制/形式在C#中的截图? [英] How can I take a screenshot of a Winforms control/form in C#?
问题描述
我有一个列表视图
控件是一个WinForms形式。它填补了全屏,但也有更多的项目有比屏幕可以显示。
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.
我怎么能取整控制,如果我能显示列表视图
在屏幕上的全部内容的截图?因此,如果整个列表视图
采用1000×4000像素,那么我想该尺寸的图像/位图
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.
我如何做到这一点?当我尝试PRINTSCREEN,它只返回什么是在屏幕上,屏幕以外的任何东西显示为灰色。
How do I do this? When I try printscreen, it only returns what's on the screen and anything outside the screen appears grey.
推荐答案
表单是控制,所以你应该能够全部内容复制到一个位图保存是这样的:
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);
更新
DrawToBitmap
只汲取什么是在屏幕上。如果你想吸引你必须遍历列表列表的全部内容找到内容的大小,然后绘制每个项目。是这样的:
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;
}
}
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);
}
这篇关于我怎么可以把一个WinForms控制/形式在C#中的截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!