Solved

PrintWindow() screenshots and transparency

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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