gpt4 book ai didi

c# - control.DrawToBitmap() 未按预期工作

转载 作者:行者123 更新时间:2023-11-30 22:21:56 25 4
gpt4 key购买 nike

在 Winform 应用程序中,我有一个用户控件;在其上绘制矩形,圆形等形状。我正在尝试使用 DrawToBitmap() 方法拍摄相同的快照。我有一个固定大小(300 x 300)的位图,用户控件的大小为(600 x 800)因此拍摄的快照仅包含部分用户控件。

如何获取位图中整个用户控件的快照?提前致谢。

最佳答案

您可以使用以下方法:

static void DrawControlToImage(Control ctrl, Image img) {
Rectangle sourceRect = ctrl.ClientRectangle;
Size targetSize = new Size(img.Width, img.Height);
using(Bitmap tmp = new Bitmap(sourceRect.Width, sourceRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
ctrl.DrawToBitmap(tmp, sourceRect);
using(Graphics g = Graphics.FromImage(img)) {
g.DrawImage(tmp, new Rectangle(Point.Empty, targetSize));
}
}
}

关于c# - control.DrawToBitmap() 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14052725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com