Solved

PrintWindow() screenshots and transparency

Posted on 2007-11-19
4
361 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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