慕勒3428872
这些方法集允许将ScrollableControl的内容打印到位图。程序描述:控件首先滚动回原点(control.AutoScrollPosition = new Point(0, 0);(否则会引发异常:位图大小错误。您可能需要存储当前滚动位置并在之后恢复)。验证并存储由PreferredSize或DisplayRectangle属性返回的 Container 的实际大小(取决于方法参数设置的条件和打印的容器类型)。此属性考虑容器的完整范围。这将是位图的大小。使用容器的背景颜色清除位图。迭代ScrollableControl.Controls集合并按其相对位置打印所有第一级子控件(子控件的Bounds矩形相对于容器 ClientArea。)如果第一级控件有子控件,则调用DrawNestedControls递归方法,该方法将枚举并绘制所有嵌套的子容器/控件,并保留内部剪辑边界。包括对 RichTextBox 控件的支持。该类RichEditPrinter包含打印 RichTextBox/RichEdit 控件内容所需的逻辑。该类EM_FORMATRANGE使用正在打印控件的位图的设备上下文向 RichTextBox 发送消息。该ScrollableControlToBitmap()方法仅采用ScrollableControl类型作为参数:您不能传递 TextBox 控件,即使它使用 ScrollBars。▶ 将fullSize参数设置为true或false以包含容器内的所有子控件或仅包含可见的子控件。如果设置为true,则容器ClientRectangle将展开以包含并打印其所有子控件。▶ 将includeHidden参数设置为true或false以包含或排除隐藏控件(如果有)。注意:此代码使用Control.DeviceDpi属性来评估容器设备上下文的当前 Dpi。此属性需要 .Net Framework 4.7+。如果此版本不可用,您可以删除:bitmap.SetResolution(canvas.DeviceDpi, canvas.DeviceDpi);可能的话,更新项目的框架版本:)// Prints the content of the current Form instance, // include all child controls and also those that are not visiblevar bitmap = ControlPrinter.ScrollableControlToBitmap(this, true, true);// Prints the content of a ScrollableControl inside a Form// include all child controls except those that are not visiblevar bitmap = ControlPrinter.ScrollableControlToBitmap(this.panel1, true, false);using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;using System.Windows.Forms;public class ControlPrinter{ public static Bitmap ScrollableControlToBitmap(ScrollableControl canvas, bool fullSize, bool includeHidden) { canvas.AutoScrollPosition = new Point(0, 0); if (includeHidden) { canvas.SuspendLayout(); foreach (Control child in canvas.Controls) { child.Visible = true; } canvas.ResumeLayout(true); } canvas.PerformLayout(); Size containerSize = canvas.DisplayRectangle.Size; if (fullSize) { containerSize.Width = Math.Max(containerSize.Width, canvas.ClientSize.Width); containerSize.Height = Math.Max(containerSize.Height, canvas.ClientSize.Height); } else { containerSize = canvas.ClientSize;; } var bitmap = new Bitmap(containerSize.Width, containerSize.Height, PixelFormat.Format32bppArgb); bitmap.SetResolution(canvas.DeviceDpi, canvas.DeviceDpi); var graphics = Graphics.FromImage(bitmap); if (canvas.BackgroundImage != null) { graphics.DrawImage(canvas.BackgroundImage, new Rectangle(Point.Empty, containerSize)); } else { graphics.Clear(canvas.BackColor); } var rtfPrinter = new RichEditPrinter(graphics); try { DrawNestedControls(canvas, canvas, new Rectangle(Point.Empty, containerSize), bitmap, rtfPrinter); return bitmap; } finally { rtfPrinter.Dispose(); graphics.Dispose(); } } private static void DrawNestedControls(Control outerContainer, Control parent, Rectangle parentBounds, Bitmap bitmap, RichEditPrinter rtfPrinter) { for (int i = parent.Controls.Count - 1; i >= 0; i--) { var ctl = parent.Controls[i]; if (!ctl.Visible || (ctl.Width < 1 || ctl.Height < 1)) continue; var clipBounds = Rectangle.Empty; if (parent.Equals(outerContainer)) { clipBounds = ctl.Bounds; } else { Size scrContainerSize = parentBounds.Size; if ((parent != ctl) && parent is ScrollableControl scrctl) { if (scrctl.VerticalScroll.Visible) scrContainerSize.Width -= (SystemInformation.VerticalScrollBarWidth + 1); if (scrctl.HorizontalScroll.Visible) scrContainerSize.Height -= (SystemInformation.HorizontalScrollBarHeight + 1); } clipBounds = Rectangle.Intersect(new Rectangle(Point.Empty, scrContainerSize), ctl.Bounds); } if (clipBounds.Width < 1 || clipBounds.Height < 1) continue; var bounds = outerContainer.RectangleToClient(parent.RectangleToScreen(clipBounds)); if (ctl is RichTextBox rtb) { rtfPrinter.DrawRtf(rtb.Rtf, outerContainer.Bounds, bounds, ctl.BackColor); } else { ctl.DrawToBitmap(bitmap, bounds); } if (ctl.HasChildren) { DrawNestedControls(outerContainer, ctl, clipBounds, bitmap, rtfPrinter); } } } internal class RichEditPrinter : IDisposable { Graphics dc = null; RTBPrinter rtb = null; public RichEditPrinter(Graphics graphics) { this.dc = graphics; this.rtb = new RTBPrinter() { ScrollBars = RichTextBoxScrollBars.None }; } public void DrawRtf(string rtf, Rectangle canvas, Rectangle layoutArea, Color color) { rtb.Rtf = rtf; rtb.Draw(dc, canvas, layoutArea, color); rtb.Clear(); } public void Dispose() => this.rtb.Dispose(); private class RTBPrinter : RichTextBox { public void Draw(Graphics g, Rectangle hdcArea, Rectangle layoutArea, Color color) { using (var brush = new SolidBrush(color)) { g.FillRectangle(brush, layoutArea); }; IntPtr hdc = g.GetHdc(); var canvasAreaTwips = new RECT().ToInches(hdcArea); var layoutAreaTwips = new RECT().ToInches(layoutArea); var formatRange = new FORMATRANGE() { charRange = new CHARRANGE() { cpMax = -1, cpMin = 0 }, hdc = hdc, hdcTarget = hdc, rect = layoutAreaTwips, rectPage = canvasAreaTwips }; IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(formatRange)); Marshal.StructureToPtr(formatRange, lParam, false); SendMessage(this.Handle, EM_FORMATRANGE, (IntPtr)1, lParam); Marshal.FreeCoTaskMem(lParam); g.ReleaseHdc(hdc); } [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)] internal static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam); internal const int WM_USER = 0x0400; // https://learn.microsoft.com/en-us/windows/win32/controls/em-formatrange internal const int EM_FORMATRANGE = WM_USER + 57; [StructLayout(LayoutKind.Sequential)] internal struct RECT { public int Left; public int Top; public int Right; public int Bottom; public Rectangle ToRectangle() => Rectangle.FromLTRB(Left, Top, Right, Bottom); public RECT ToInches(Rectangle rectangle) { float inch = 14.92f; return new RECT() { Left = (int)(rectangle.Left * inch), Top = (int)(rectangle.Top * inch), Right = (int)(rectangle.Right * inch), Bottom = (int)(rectangle.Bottom * inch) }; } } // https://learn.microsoft.com/en-us/windows/win32/api/richedit/ns-richedit-formatrange? [StructLayout(LayoutKind.Sequential)] internal struct FORMATRANGE { public IntPtr hdcTarget; // A HDC for the target device to format for public IntPtr hdc; // A HDC for the device to render to, if EM_FORMATRANGE is being used to send the output to a device public RECT rect; // The area within the rcPage rectangle to render to. Units are measured in twips. public RECT rectPage; // The entire area of a page on the rendering device. Units are measured in twips. public CHARRANGE charRange; // The range of characters to format (see CHARRANGE) } [StructLayout(LayoutKind.Sequential)] internal struct CHARRANGE { public int cpMin; // First character of range (0 for start of doc) public int cpMax; // Last character of range (-1 for end of doc) } } }}