Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with getting a screenshot in Win 10 from within TIdHTTPServer GET handler
#1
Hi,
I am having a weird issue regarding taking a desktop screenshot and sending it via http GET request on Windows 10. This works fine on windows 7 but I don't know for some reason it does not work the same on windows 10. It always produces a white image.

When I use the same function outside the GET event it works fine on Win10, at first I thought it did not work because of the function is called inside a separate thread but I tested it with another thread call and it worked just fine but does not work with Indy HTTP GET thread.

I don't know if it is an Indy's issue because I tried everything and nothing works.

Here is the code:

Code:
TStream* GetDesktopScreenShotStream(float Width, float Height)
{
TMemoryStream *Stream = new TMemoryStream;

HDC DC = GetDC(0);
try
{
unique_ptr<Vcl::Graphics::TBitmap> VCLBitmap(new Vcl::Graphics::TBitmap);
VCLBitmap->PixelFormat = pf24bit;
VCLBitmap->SetSize(Width, Height);

BitBlt(VCLBitmap->Canvas->Handle, 0,0, VCLBitmap->Width, VCLBitmap->Height, DC, 0,0, SRCCOPY);
VCLBitmap->SaveToStream(Stream);
Stream->Position = 0;
}
__finally
{
ReleaseDC(0, DC);
}

return Stream;
}
//---------------------------------------------------------------------------

Code:
void TForm1::SaveScreen()
{
std::auto_ptr<TMemoryStream> Stream((TMemoryStream*)GetDesktopScreenShotStream(Screen->Width, Screen->Height));
Stream->SaveToFile("test.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdHTTPServer1CommandGet(TIdContext *AContext, TIdHTTPRequestInfo *ARequestInfo,
  TIdHTTPResponseInfo *AResponseInfo)
{
AResponseInfo->ContentStream = GetDesktopScreenShotStream(Screen->Width, Screen->Height);
AResponseInfo->ContentType = "image/bmp";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Thread(FuncBind( &TForm1::SaveScreen, this)); //works here in a thread
}
//---------------------------------------------------------------------------

What am I doing wrong? Any help will be appreciated.
Thanks in advance
Reply


Messages In This Thread
Issue with getting a screenshot in Win 10 from within TIdHTTPServer GET handler - by Ahmed Sayed - 06-29-2022, 04:31 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)