?
Solved

How to capture screen to TBitmap in c++builder?

Posted on 2002-06-08
1
Medium Priority
?
710 Views
Last Modified: 2008-02-01
Hello all!

How to capture screen to TBitmap in c++builder?

thanks! :-)

0
Comment
Question by:Soft_Ware_Pro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
magellanLS earned 300 total points
ID: 7064768
Hello!

The following code should do the trick:

void __fastcall TForm1::CaptureBtnClick(TObject *Sender)
{
// ensure no other form shadows this form
   FormStyle = fsStayOnTop;
   Refresh();
// Create a TCanvas object for the desktop DC.
   TCanvas* dtCanvas = new TCanvas;
   dtCanvas->Handle = GetDC(0);
// Create a new TBitmap object and set its
// size to the size of the form.
   Graphics::TBitmap* bitmap = new Graphics::TBitmap;
   bitmap->Width = Width;
   bitmap->Height = Height;

// Create a palette from the form's Canvas
// and assign that palette to the bitmap's
// Palette property.
   int nColors = GetDeviceCaps(Canvas->Handle,SIZEPALETTE);
   LOGPALETTE* logPal = (LOGPALETTE*)new Byte[sizeof(LOGPALETTE) +(nColors - 1) *sizeof(PALETTEENTRY)];
   logPal->palVersion  = 0x300;
   logPal->palNumEntries = (Word)nColors;
   GetSystemPaletteEntries(Canvas->Handle,0,nColors,logPal->palPalEntry);
   bitmap->Palette = CreatePalette(logPal);
   delete[] logPal;

// Copy a section of the screen from the
// desktop canvas to the bitmap.
   TRect src = BoundsRect;
   TRect dest = Rect(0, 0, Width, Height);
   bitmap->Canvas->CopyRect(dest, dtCanvas, src);

// Save it to disk.
   char buffer[256];
   GetWindowsDirectory(buffer, sizeof(buffer));
   AnsiString asFileName = FileSearch(ToFileEdit->Text + ".bmp",
        GetCurrentDir() + AnsiString(";") + AnsiString(buffer));
   if (asFileName.IsEmpty())
      bitmap->SaveToFile(ToFileEdit->Text + ".bmp");
   else
     Application->MessageBox("Choose another name","File Exists",MB_OK|MB_ICONERROR);
// Clean up and go home.
   delete bitmap;

Hope this helps!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

764 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