<分区>
我有一个表单,其中有一个覆盖控件(透明的灰色背景,“拖放到此处...”上带有白色文本和一个图标),只有在将文件拖到表单上时才可见。通过在其背面绘制控件然后用透明灰色 (ARGB) 填充,可以使叠加层透明。当 Overlay 应该覆盖在不是 Form 的 Control 上时,该方法非常有效,但是当我使用 Control.DrawToBitmap
呈现 Form 而不是通常的 Control 时,它还会呈现标题栏和边框。
<分区>
我有一个表单,其中有一个覆盖控件(透明的灰色背景,“拖放到此处...”上带有白色文本和一个图标),只有在将文件拖到表单上时才可见。通过在其背面绘制控件然后用透明灰色 (ARGB) 填充,可以使叠加层透明。当 Overlay 应该覆盖在不是 Form 的 Control 上时,该方法非常有效,但是当我使用 Control.DrawToBitmap
呈现 Form 而不是通常的 Control 时,它还会呈现标题栏和边框。
最佳答案
Form.DrawToBitmap
绘制整个表单,包括非客户区。您可以使用 BitBlt
. BitBlt 函数将对应于矩形像素的颜色数据从指定的源设备上下文执行位 block 传输到目标设备上下文。
const int SRCCOPY = 0xCC0020;
[DllImport("gdi32.dll")]
static extern int BitBlt(IntPtr hdc, int x, int y, int cx, int cy,
IntPtr hdcSrc, int x1, int y1, int rop);
Image PrintClientRectangleToImage()
{
var bmp = new Bitmap(ClientSize.Width, ClientSize.Height);
using (var bmpGraphics = Graphics.FromImage(bmp))
{
var bmpDC = bmpGraphics.GetHdc();
using (Graphics formGraphics = Graphics.FromHwnd(this.Handle))
{
var formDC = formGraphics.GetHdc();
BitBlt(bmpDC, 0, 0, ClientSize.Width, ClientSize.Height, formDC, 0, 0, SRCCOPY);
formGraphics.ReleaseHdc(formDC);
}
bmpGraphics.ReleaseHdc(bmpDC);
}
return bmp;
}
关于c# - 如何使用 Control.DrawToBitmap 将 Form 渲染为没有装饰(标题栏、边框)的 Bitmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54628992/