Đây không phải là câu trả lời chính xác, nhưng đây là một đoạn mã có ảnh chụp màn hình (ảnh bitmap) của điều khiển Bảng điều khiển cuộn trên Biểu mẫu. Hạn chế lớn nhất là màn hình nhấp nháy trong khi chụp màn hình. Tôi đã thử nghiệm nó trên các ứng dụng đơn giản, vì vậy nó có thể không hoạt động trong mọi trường hợp, nhưng đó có thể là một sự khởi đầu.
Dưới đây là làm thế nào để sử dụng nó:
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");
}
}
Và đây là các tiện ích:
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);
}
}
Nguồn
2013-01-02 09:54:53
Nó không phải là một người bị mắc mưu chính xác, nhưng vấn đề cơ bản là giống nhau. Không có giải pháp dễ dàng ở đây. Những gì bạn muốn in không thực sự tồn tại (vì nó chưa được vẽ). Bạn sẽ phải cuộn biểu mẫu và chụp nhiều ảnh trước khi in. Việc in dữ liệu có thể dễ dàng hơn mà không phải lo lắng quá nhiều về việc khớp chính xác với biểu mẫu. –
@ JonB - vâng, loại câu hỏi này có thể giúp tìm câu trả lời chung cho vấn đề tái diễn này. Các bản sao có thể có vẻ không áp dụng thực sự tốt. Là "nó không thể" câu trả lời duy nhất bao giờ này trên SO? –