Solved

PrintWindow() screenshots and transparency

Posted on 2007-11-19
4
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
Migration of Exchange mailbox can be done with the ExProfre.exe tool. But at times, when the ExProfre.exe tool migrates the Exchange Server user profile, it results in numerous synchronization problems. Synchronization error messages appear in the e…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

739 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