Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# DrawToBitmap alternative only working every second time
New Posts  All Forums:Forum Nav:

C# DrawToBitmap alternative only working every second time

post #1 of 2
Thread Starter 
Hi smile.gif

I'm trying to capture screenshots of panels, containing webbrowsers.
Surprisingly after all that I read on the Internet, DrawToBitmap does work in most cases. However some certain pages in the webbrowser render all white.

I have a small method to check if the screenshot is all white, and if so, to use an alternate method to capture the image. This "CheckAllWhite" method works fine, however when I try to use the alternate capture method, it always works the second time.

Say for example, I have two panels with one webbrowser each, one viewing "google.com", one viewing "youtube.com". Those both are sites that render white with the original DrawToBitmap.
The alternate method only works if the control is topmost and currently visible. So when I find that I have to re-capture, I bring the panel that I want to capture to front, and then try DrawToImage.
Here's the code for that: ("t" is the panel)
Code:
Bitmap b = new Bitmap(size, size);
t.DrawToBitmap(b, t.ClientRectangle);
if (Utls.CheckAllWhite(b))
      {
            t.BringToFront();
            b = (Bitmap)Utls.DrawToImage(t);
      }
panelThumb.BackgroundImage = b;

And here is the code of the DrawToImage itself: (found from the net and fixed a little to give square image)
Code:
public static Image CaptureWindow(IntPtr handle)
        {

            IntPtr hdcSrc = GetWindowDC(handle);

            RECT windowRect = new RECT();
            GetWindowRect(handle, windowRect);

            int width = windowRect.right - windowRect.left;
            int height = windowRect.bottom - windowRect.top;
            int size = Math.Min(width, height);

            IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
            IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, size, size);

            IntPtr hOld = SelectObject(hdcDest, hBitmap);
            
            BitBlt(hdcDest, 0, 0, size, size, hdcSrc, 0, 0, 13369376);
            SelectObject(hdcDest, hOld);
            DeleteDC(hdcDest);
            ReleaseDC(handle, hdcSrc);

            Image image = Image.FromHbitmap(hBitmap);
            DeleteObject(hBitmap);

            return image;
        }

public static Image DrawToImage(this Control control)
        {
            return CaptureWindow(control.Handle);
        }

The problem is - if I try to capture the panel that is already top-most, it works.
If I try to capture a background panel, it captures the other one, despite the fact that I call .BringToFront() before the capture.
When I try again to capture the same panel (the one that was originally background, but that I brought to front with the last call), after it already is top-most, it works.

So generally it acts as if I call .BringToFront() at the end of the method.

Any clue how to fix that? Hackish approaches are acceptable biggrin.gif
Edited by ronnin426850 - 11/11/12 at 7:04am
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
post #2 of 2
Thread Starter 
Solved with the following idiotic approach:
Code:
private delegate void ToInvoke();
...

Thread th = new Thread(delegate() {
      Bitmap b = new Bitmap(size, size);
      t.DrawToBitmap(b, t.ClientRectangle);
      if (Utls.CheckAllWhite(b))
            {
                  t.Invoke(new ToInvoke(t.BringToFront));
                  Thread.Sleep(50);
                  b = (Bitmap)Utls.DrawToImage(t);
            }
      panelThumb.BackgroundImage = b;
});
th.IsBackground = true; th.Start();


Edited by ronnin426850 - 11/11/12 at 8:01am
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
R.E.D (dead)
(5 items)
 
Wife's D3 machine
(13 items)
 
 
CPUGraphicsRAMHard Drive
Core 2 Duo P8400 Nvidia 9600M GT 2x2Gb Hynx DDR2 WD Scorpio Blue 
OS
Windows 7 x64 Professional 
CPUMotherboardGraphicsRAM
Intel Pentium G840 DualCore SB 2.8Ghz AsRock H61M-VS Inno3D 8800 GTS 320 Kingston HyperX 1600 4Gb 
Hard DriveOptical DriveCoolingOS
WD Scorpio Blue 250Gb LiteOn DVD-RW stock Win 7 Home Premium 
MonitorKeyboardPowerCase
Acer 19" 1440x900 Logitech MX3200 beQuiet 300W MS-Tech CA-0130 Black 
Mouse
Logitech MX300 Laser 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C# DrawToBitmap alternative only working every second time