【发布时间】:2011-02-03 03:21:07
【问题描述】:
我在 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,但似乎没有任何效果。
【问题讨论】:
-
你可能想要一个 C# 标签。
标签: winforms graphics winapi gdi+ gdi