Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

730 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