Solved

PrintWindow() screenshots and transparency

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Can you find a fax from a vendor you saved a decade ago in seconds? Have you ever cursed your PC under your breath during an audit because you couldn’t find the requested statement or driver history?  If you answered no to the first question or yes …
cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, rese…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

627 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