0

I use windows 7 and VS2010. I write a piece of C++/CLI code to do measurements, during which I want to make screenshot.

I have tried screenshot method in C++ (using GDI library), but failed to compile the file. So I suppose the GDI library could not be used in C++/CLI. How to do screenshot and paste to a word in C++/CLI project?

Here is the code:

#include "stdafx.h"

using namespace System;
using namespace System::IO;

int main(array<System::String ^> ^args)
{

String^ fileName = "AP_result.doc";

StreamWriter^ sw = gcnew StreamWriter(fileName);
 ...
sw->Close();
 return 0;
}

I have tried Slawomir Orlowski's method but error occurs:

error

Bill
  • 33
  • 7

2 Answers2

0

As explained here

https://stackoverflow.com/a/3291411

you need to get context of screen, then put it in bitmap file, it's better explained in answer above, so check answers and comments

  • thanks. I have tried all code in that page, but none of them work for me. I use C++/CLI. In that page, people use C++. – Bill Jan 17 '19 at 07:01
0

C++/CLI code for screen shot:

using namespace System;
using namespace System::Windows;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Drawing::Imaging;


int main(array<System::String ^> ^args)
{
    Screen^ r = System::Windows::Forms::Screen::PrimaryScreen;
    int width = r->Bounds.Width;
    int height = r->Bounds.Height;

    Bitmap^ bmp = gcnew Bitmap(width, height, PixelFormat::Format32bppArgb);
    Graphics^ screen = Graphics::FromImage(bmp);
    screen->CopyFromScreen(0,0,0,0, Size(width, height), CopyPixelOperation::SourceCopy);
    bmp->Save("screenshot.bmp");      
    return 0;
}

In your project you have to add few references: System.Drawing, System.Windows, System.Windows.Forms.

  • Method works fine for me. I have tested it. What kind of error did you get? – Slawomir Orlowski Jan 17 '19 at 08:03
  • Please see my updated question, which list the error using your method. – Bill Jan 17 '19 at 08:07
  • Mate, you have to add additional rederences to your project: System.Drawing, System.Windows, System.Windows.Forms. I have already written this in my answer. Right click on references in project->Add references... In Assemblies tip: System.Drawing, System.Windows and System.Windows.Forms. Did you do it? – Slawomir Orlowski Jan 17 '19 at 08:15
  • Thanks, bro, it worked well after adding references. – Bill Jan 17 '19 at 08:27