Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, my aim is to capture the screen of a windows form using c++/cli. Below is the code to capture the window, however, it is in C#. What changes do I have to make to the code for it to work in c++?


Graphics myGraphics = this.CreateGraphics();
       Size s = this.Size;
       memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
       Graphics memoryGraphics = Graphics.FromImage(memoryImage);
       memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);


What I have tried:

I've tried using the code below in c++, however, I get errors for the parts in bold.
For the first bold part, I get an error that says expected a ; after Size, for the graphics error, it says type name is not allowed .

Graphics^ myGraphics = this->CreateGraphics();
	Size s = this->Size;
	memoryImage = gcnew Bitmap(s->Width, s->Height, myGraphics);
	Graphics^ memoryGraphics = Graphics->FromImage(memoryImage);
	memoryGraphics->CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, s);
Posted
Updated 16-Jun-21 23:44pm

1 solution

You are treating the class name as a pointer by using the -> operator to dereference a static member method.
Try this:
C++
Graphics^ memoryGraphics = Graphics::FromImage(memoryImage);

If you had gone to the MS documentation and selected C++ as the sample language, it would have shown you how to use it... Graphics.FromImage(Image) Method (System.Drawing) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 15248812 17-Jun-21 5:00am    
Thank you, the graphics error is now gone. Do you have any idea why I get an error for Size s = this->Size?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900