Andreas
Andreas

Reputation: 692

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. 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.

Upvotes: 0

Views: 602

Answers (1)

Andreas
Andreas

Reputation: 692

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.

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

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

Upvotes: 1

Related Questions