Link to home
Start Free TrialLog in
Avatar of kevindelaney
kevindelaneyFlag for United States of America

asked on

Capture screenshot as JPEG and store in memory

Hi there!

I'm looking to take a screenshot of the active window as a JPEG, and store it in memory so that I can later send it via an HTTP POST operation. I need to be able to manually adjust the quality so I can control the file size. I tried to do some reading on the jpeg library, but coming from a .NET background, I couldn't really figure it out. Is the one that comes with Delphi the best one to use for this situation?

Thanks for any and all assistance!
Avatar of wd123
wd123
Flag of Belarus image

example to use
var
  Image: TJPEGImage;
begin
  Image := TJPEGImage.Create;
  try
    if GetScreenShot(GetDesktopWindow, 70, Image) then Image1.Picture.Assign(Image);
  finally
    Image.Free;
  end;

uses jpeg;
function GetScreenShot(const AHandle: THandle; const CompressPercent: Byte;
  var AImage: TJPEGImage): Boolean;
var
  fBitmap: TBitmap;
  DC: HDC;
  Rect: TRect;
begin
  Result := False;
  if AImage = nil then Exit;
  DC := GetDC(AHandle);
  if DC <> 0 then
  try
    fBitmap := TBitmap.Create;
    try
      if not GetClientRect(AHandle, Rect) then Exit;
      fBitmap.Width := Rect.Right - Rect.Left;
      fBitmap.Height := Rect.Bottom - Rect.Top;
      fBitmap.PixelFormat := pf32bit;
      Result := BitBlt(fBitmap.Canvas.Handle, 0, 0, fBitmap.Width,
        fBitmap.Height, DC, 0, 0, SRCCOPY);
      if not Result then Exit;
      AImage.Assign(fBitmap);
      AImage.CompressionQuality := CompressPercent;
    finally
      fBitmap.Free;
    end;
  finally
    ReleaseDC(AHandle, DC);
  end;
end;

Open in new window

how to send via http post a image file ?
Avatar of kevindelaney

ASKER

wd123: Thanks for the links and help. Would you be able to quickly post how I can resize the JPEG by 50%? For example if it's 1024x768 I want to reduce it to 800x600 before storing it in the memory. I've looked everywhere and can't seem to get it to work with this code. The closest thing I found was this:

https://www.experts-exchange.com/questions/21569254/How-to-resize-a-bmp-or-jpg-image.html

Which I wasn't able to get successfully working.

I will award points if you can solve this for me! (increased to 200)

Thanks again!
ASKER CERTIFIED SOLUTION
Avatar of wd123
wd123
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MerijnB
what part of this PAQ did you have problems with?

http://delphi.about.com/od/graphics/a/resize_image.htm
Great solution and fast. Worked perfectly. THANK YOU!