• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

VS C++ Express Registration

You haven't even said what doesn't work, so it's unlikely anybody will be able to help. Does it fail to compile? Does it compile but throw a runtime exception? Does it compile and run without crashing but not do what you expect it to?

VS has a very good debugger, and it's only a few lines to step through to identify the precise point of failure, at which point looking at the MSDN docs for the relevant API call will probably point you in the right direction. Search on Raymond's blog for the relevant function's name as well, as he often mentions various gotchas and he's written loads about GDI in the past.

Doesn't work as in run the code and it fails to load the screen shot into the picture box.

I have fixed it. And am posting the code back here like a good little boy.

Code:
public Bitmap CaptureScreenToPictureBox(int hDcPbx)
        {
            int hdcSrc = User32.GetWindowDC(User32.GetDesktopWindow()),
            hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
            hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,200, 200); 
            GDI32.SelectObject(hdcDest, hBitmap);
            GDI32.StretchBlt(hdcDest, 0, 0,200,200, hdcSrc, 0, 0,1200,800, 0x00CC0020);
            Bitmap screenShot = Image.FromHbitmap((IntPtr)hBitmap);
            return screenShot;
        }

This is changed to return a System.Drawing.Bitmap object

Then the call is changed thusly

Code:
Example f = new Example();
this.pictureBox1.Image = f.CaptureScreenToPictureBox(this.pictureBox1.CreateGraphics().GetHdc().ToInt32());
 
SuitYou, can I recommend StackOverflow to you? Worth a try for this kind of thing.
 
From here:

http://www.c-sharpcorner.com/UploadFile/perrylee/ScreenCapture11142005234547PM/ScreenCapture.aspx

Code:
// Create a bitmap from the Windows handle
Bitmap image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
Image.FromHbitmap(new IntPtr(hBitmap)).Width,
Image.FromHbitmap(new IntPtr(hBitmap)).Height);

This rings a very faint bell back from when I did C# last year. You had to set the width and height otherwise it didn't work, although on the face of it the above code is somewhat insane.
 
From here:

http://www.c-sharpcorner.com/UploadFile/perrylee/ScreenCapture11142005234547PM/ScreenCapture.aspx

Code:
// Create a bitmap from the Windows handle
Bitmap image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
Image.FromHbitmap(new IntPtr(hBitmap)).Width,
Image.FromHbitmap(new IntPtr(hBitmap)).Height);

This rings a very faint bell back from when I did C# last year. You had to set the width and height otherwise it didn't work, although on the face of it the above code is somewhat insane.

:D

It's not just me. :banana:

A lot of these modern framework implementations make things quite bulky, when they could be elegant.

Ponders. Another plan B?
 
There are already zillions of wrappers around frameworks. It arguably makes the framework easier, but then is one more library for a developer to learn, and means two developers may know different wrappers for the same framework.
 
All these wizardy things are great until it all goes tits up & then you have to know the stuff the wizard is based on.

Now that's magic.
 

Advertisers

Back
Top