Solved

PrintWindow() screenshots and transparency

Posted on 2007-11-19
4
355 Views
Last Modified: 2008-02-01
I was wondering how I could take screenshots using PrintWindow() on Windows XP and get the transparency information, so I don't get an ugly black border around all my individual window screenshots. This seems to be possible from looking at other programs (e.g. TopDesk, http://www.otakusoftware.com/topdesk/). I played with calling GetLayeredWindowAttributes to find out transparency information, but it only succeeds for app that initially set the transparency information in the first place.
0
Comment
Question by:mikejurka
  • 2
4 Comments
 

Author Comment

by:mikejurka
ID: 20317295
OK, figured it out! It seems like you can't get layered window information from PrintWindow. However, most windows "do transparency" via clipping window regions, which I didn't know about. I had to interop/import a few more functions relating to regions, and then I print the window to a bitmap and then draw it to another bitmap with the window region as a clipping frame. If someone can suggest a way to do this using only one bitmap, I'd really appreciate it!





            Rect rc;

            GetWindowRect(hwnd, out rc);
 

            if (rc.Width == 0 && rc.Height == 0)

            {

                return null;

            }
 

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)rc.Width, (int)rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            System.Drawing.Graphics gfxBitmap = System.Drawing.Graphics.FromImage(bitmap);
 

            IntPtr hdcBitmap = gfxBitmap.GetHdc();

            PrintWindow(hwnd, hdcBitmap, 0);

            gfxBitmap.ReleaseHdc(hdcBitmap);
 
 

            IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);

            GetWindowRgn(new IntPtr(hwnd), hRgn);

            System.Drawing.Region region = System.Drawing.Region.FromHrgn(hRgn);

            if (!region.IsEmpty(gfxBitmap))

            {

                System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap((int)rc.Width, (int)rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                System.Drawing.Graphics gfxBitmap2 = System.Drawing.Graphics.FromImage(bitmap2);

                gfxBitmap2.Clip = region;

                gfxBitmap2.DrawImage(bitmap, new System.Drawing.Point(0, 0));

                System.Drawing.Bitmap oldBitmap = bitmap;

                bitmap = bitmap2;

                oldBitmap.Dispose();

                gfxBitmap2.Dispose();
 

                //gfxBitmap.ExcludeClip(region);

                //gfxBitmap.FillRegion(new System.Drawing.SolidBrush(System.Drawing.Color.Red), region);

            }
 

            gfxBitmap.Dispose();

Open in new window

0
 

Author Comment

by:mikejurka
ID: 20317344
OK, figured it out! Here's a way to do it using only one bitmap:

            Rect rc;
            GetWindowRect(hwnd, out rc);

            if (rc.Width == 0 && rc.Height == 0)
            {
                return null;
            }

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)rc.Width, (int)rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            System.Drawing.Graphics gfxBitmap = System.Drawing.Graphics.FromImage(bitmap);

            IntPtr hdcBitmap = gfxBitmap.GetHdc();
            PrintWindow(hwnd, hdcBitmap, 0);
            gfxBitmap.ReleaseHdc(hdcBitmap);


            IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);
            GetWindowRgn(new IntPtr(hwnd), hRgn);
            System.Drawing.Region region = System.Drawing.Region.FromHrgn(hRgn);

            if (!region.IsEmpty(gfxBitmap))
            {
                gfxBitmap.ExcludeClip(region);
                gfxBitmap.Clear(System.Drawing.Color.Transparent);
               
            }

            gfxBitmap.Dispose();
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20521462
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Disclosure: Use this tutorial only when no other options helps to get Windows XP running without any problems and you don't want to format the drive. The back up of the data is the responsible of the user, however there is a description of how t…
Step by step guide to Clean and Sort your windows registry! Introduction: Always remember: A Clean registry = Better performance = Save your invaluable time In this article we're going to clear our registry manually! Yes, manually! The e…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now