Solved

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

Posted on 2002-06-08
1
709 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 75 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

726 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