如何在没有插入符号的情况下将WinForm窗口捕获到位图

如何在没有插入符号的情况下将WinForm窗口捕获到位图

本文介绍了如何在没有插入符号的情况下将WinForm窗口捕获到位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForm上有一个要获取其位图表示形式的窗口.为此,我使用以下代码(其中codeEditor是我想要的位图表示形式的控件):

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( ) ;

麻烦的是,如果捕获时插入符号在窗口中闪烁,则捕获捕获符.我尝试在捕获前调用Win32方法HideCaret,但似乎没有任何作用.

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.

推荐答案

好吧,一种方法是将焦点设置为表单的其他控件-稍后可能将焦点恢复到文本字段.

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.

这篇关于如何在没有插入符号的情况下将WinForm窗口捕获到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:57