Solved

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

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

Accepted Solution

by:
magellanLS earned 75 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now