简体   繁体   English

C#WinForm重定向绘制到位图,不显示任何内容

[英]C# WinForm redirect draw to bitmap, nothing to screen

I'm wondering if there is any way to disable screen-drawing for a winform and only draw to a bitmap. 我想知道是否有任何方法可以禁用Winform的屏幕绘制,而只能绘制到位图。 What I'm actually trying to achieve is to create a "live image" based on a form but without having to actually having the form visible. 我实际上想要实现的是基于表单创建“实时图像”,而不必实际使表单可见。

I tried DrawToBitmap while the form was minimized, but this was highly unstable, didn't work and finally crashed. 我在最小化表单时尝试了DrawToBitmap,但这非常不稳定,无法正常工作,最终崩溃了。

Ok so I ended up solving this in a bit different way. 好的,所以我最终以不同的方式解决了这个问题。 Following code gets you a Live Messenger-like taskbar thumbnail by drawing you hidden UserControl to a Bitmap and using that as the thumbnail. 以下代码通过将隐藏的UserControl绘制到位图并将其用作缩略图,可以使您获得类似于Live Messenger的任务栏缩略图。

Holding your mouse over the taskbar icon still gets you some small thing in the upper left corner. 将鼠标悬停在任务栏图标上,仍会在左上角留下一些小东西。 Doesn't bother me but please tell me if you know how to get rid of it! 不会打扰我,但是请告诉我您是否知道如何摆脱它!

Make sure you have the Windows API Code Pck from Microsoft to run this http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906 确保您具有Microsoft的Windows API代码Pck来运行此http://archive.msdn.microsoft.com/WindowsAPICodePack/Release/ProjectReleases.aspx?ReleaseId=4906

namespace AndreasCoroiu.Controls
{
    public partial class TaskbarThumbnail : UserControl
    {
        TaskbarForm taskbarForm;
        public TaskbarThumbnail()
        {
            InitializeComponent();
            if (!DesignMode)
            {
                taskbarForm = new TaskbarForm();
                TabbedThumbnail preview = new TabbedThumbnail(taskbarForm.Handle, taskbarForm.Handle);
                TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
                preview.TabbedThumbnailBitmapRequested += (o, e) =>
                {
                    Bitmap bmp = new Bitmap(Width, Height);
                    DrawToBitmap(bmp, new Rectangle(new Point(0, 0), bmp.Size));
                    preview.SetImage(bmp);
                    e.Handled = true;
                };
            }
        }

        public void Show()
        {
            taskbarForm.Show();
        }

        private class TaskbarForm : Form
        {
            public TaskbarForm()
                : base()
            {
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            }

            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                Size = new System.Drawing.Size(0, 0);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM