• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Save a Region of Screen in Jpeg

I have to save a region of desktop directly in jpeg
0
wackyteseo
Asked:
wackyteseo
  • 2
2 Solutions
 
spiderwilk007Commented:
I usually do a print screen (press the prnt scrn button) paste it into paint or another editing program (ctrl v) and crop it to the section you want, then save it as a JPEG.
0
 
Ephraim WangoyaCommented:

You can use this code
implementation

uses
  JPeg;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  DC: HDC;
  Pic: TJpegImage;
  Bitmap: TBitmap;
  Stream: TFileStream;
begin
  DC := GetDC (GetDesktopWindow) ;
  try
    Pic := TJpegImage.Create;
    Bitmap := TBitmap.Create;
    try
      Bitmap.Width := GetDeviceCaps (DC, HORZRES) ;
      Bitmap.Height := GetDeviceCaps (DC, VERTRES) ;

      //you can specify the section you want to save into the bitmap here
      BitBlt(Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, DC, 0, 0, SRCCOPY) ;

      //transfer image to jpeg
      Pic.Assign(Bitmap);

      //save image to file
      Stream := TFileStream.Create('c:\test.jpg', fmCreate);
      try
        Pic.SaveToStream(Stream);
      finally
        Stream.free;
      end;
    finally
      FreeAndNil(Pic);
      FreeAndNil(Bitmap);
    end;
  finally
    ReleaseDC (GetDesktopWindow, DC) ;
  end;
end;

Open in new window

0
 
Ephraim WangoyaCommented:
You can turn it into a procedure if you want
implementation

{$R *.dfm}

uses
  JPeg;

procedure SaveDesktopAsJPEG(const AFileName: string);
var
  DC: HDC;
  Pic: TJpegImage;
  Bitmap: TBitmap;
  Stream: TFileStream;
begin
  DC := GetDC (GetDesktopWindow) ;
  try
    Pic := TJpegImage.Create;
    Bitmap := TBitmap.Create;
    try
      Bitmap.Width := GetDeviceCaps (DC, HORZRES) ;
      Bitmap.Height := GetDeviceCaps (DC, VERTRES) ;

      //you can specify the section you want to save into the bitmap here
      BitBlt(Bitmap.Canvas.Handle, 0, 0, Bitmap.Width, Bitmap.Height, DC, 0, 0, SRCCOPY) ;

      //transfer image to jpeg
      Pic.Assign(Bitmap);

      //save image to file
      Stream := TFileStream.Create(AFileName, fmCreate);
      try
        Pic.SaveToStream(Stream);
      finally
        Stream.Free;
      end;
    finally
      FreeAndnil(Pic);
      FreeAndNil(Bitmap);
    end;
  finally
    ReleaseDC (GetDesktopWindow, DC) ;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SaveDesktopAsJPEG('c:\test1.jpg');
end;

Open in new window

0
 
jimyXCommented:
Normally you capture the screen in a Bitmap, so what you can do is, save the captured image in a Bitmap and then assign it to a Jpeg and last thing save it to a file.
uses  JPEG;

...

var
  Bmp :TBitmap;
  JpegImg :TJpegImage;
begin
 Bmp:=TBitmap.Create;
 JpegImg := TJpegImage.Create;

// capture the screen in the bitmap Bmp

...

  JpegImg.Assign(Bmp);              // then assign the Bmp to the Jpeg
  JpegImg.SaveToFile('Path to the output file.jpg');        // save the Jpeg to a file

 Bmp.Free;
 JpegImg.Free;
end;

Open in new window


Check this out for how to grab a selected area of the screen.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now