如何将 WinForm 窗口捕获为不带插入符号的位图
我在 WinForm 上有一个窗口,我想获取其位图表示形式。 为此,我使用以下代码(其中 codeEditor
是我想要其位图表示形式的控件):
public Bitmap GetBitmap( )
{
IntPtr srcDC = NativeMethods.GetDC( codeEditor.Handle ) ;
var bitmap = new Bitmap( codeEditor.Width, codeEditor.Height ) ;
Graphics graphics = Graphics.FromImage( bitmap ) ;
var deviceContext = graphics.GetHdc( ) ;
bool blitted = NativeMethods.BitBlt(
deviceContext,
0,
0,
bitmap.Width,
bitmap.Height,
srcDC,
0,
0,
0x00CC0020 /*SRCCOPY*/ ) ;
if ( !blitted )
{
throw new InvalidOperationException(
@"The bitmap could not be generated." ) ;
}
int result = NativeMethods.ReleaseDC( codeEditor.Handle, srcDC ) ;
if ( result == 0 )
{
throw new InvalidOperationException( @"Cannot release bitmap resources." ) ;
}
graphics.ReleaseHdc( deviceContext ) ;
graphics.Dispose( ) ;
问题是,如果插入符号在捕获时在窗口中闪烁,则会捕获它。我尝试在捕获之前调用Win32方法HideCaret
,但似乎没有任何效果。
I've got window on a WinForm that I want to get the bitmap representation of.
For this, I use the following code (where codeEditor
is the control I want a bitmap representation of):
public Bitmap GetBitmap( )
{
IntPtr srcDC = NativeMethods.GetDC( codeEditor.Handle ) ;
var bitmap = new Bitmap( codeEditor.Width, codeEditor.Height ) ;
Graphics graphics = Graphics.FromImage( bitmap ) ;
var deviceContext = graphics.GetHdc( ) ;
bool blitted = NativeMethods.BitBlt(
deviceContext,
0,
0,
bitmap.Width,
bitmap.Height,
srcDC,
0,
0,
0x00CC0020 /*SRCCOPY*/ ) ;
if ( !blitted )
{
throw new InvalidOperationException(
@"The bitmap could not be generated." ) ;
}
int result = NativeMethods.ReleaseDC( codeEditor.Handle, srcDC ) ;
if ( result == 0 )
{
throw new InvalidOperationException( @"Cannot release bitmap resources." ) ;
}
graphics.ReleaseHdc( deviceContext ) ;
graphics.Dispose( ) ;
The trouble is, this captures the caret if it's flashing in the window at the time of capture. I tried calling the Win32 method HideCaret
before capturing, but it didn't seem to have any effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,一种方法是将焦点设置到表单的其他一些控件 - 并可能稍后将焦点恢复到文本字段。
Well, one way is to set a focus to some other control of a form - and possibly restore the focus to a text field later on.
当你这样做时会发生什么?
What happens when you just do this?