1

I am currently making a desktop screenshot in pascal using lazarus, I have the screenshot working but it only shows the top left of the desktop. I have it set to display smaller image of the desktop on a TImage. I tried using MyBitmap.width := Round(370) and MyBitmap.Height := Round(240);

But those did not work.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, LCLIntf, LCLType;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);

  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }



procedure TForm1.Button1Click(Sender: TObject);

  var
    MyBitmap : Tbitmap;
    ScreenDC: HDC;


begin


  try
  MyBitmap := TBitmap.Create;
  ScreenDC := GetDC(0);
  MyBitmap.LoadFromDevice(ScreenDC);
  MyBitmap.Width := Round(370);
  Mybitmap.Height := Round(240);
  ReleaseDC(0, ScreenDC);
  Image1.Picture.Bitmap.Assign(MyBitmap);
  finally
    MyBitmap.free;
  end;





end;

end. 
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
andyADD
  • 610
  • 1
  • 6
  • 20
  • So what happens ? does your original MyBitmap only have part of the desktop and which part ? Or does your resulting bitmap AFTER resizing contain only part of the original bitmap ? And why do you use Round functions there ? btw, this is the 3rd result for searchign your question in web search: http://www.tek-tips.com/viewthread.cfm?qid=81626 – Arioch 'The Jun 20 '13 at 09:35
  • Google results for your question: http://www.swissdelphicenter.ch/torry/showcode.php?id=1566 http://www.delphipages.com/forum/showthread.php?t=203794 http://delphi.about.com/od/graphics/a/resize_image.htm http://stackoverflow.com/questions/1976116 http://stackoverflow.com/questions/2788049 Well, perhaps it makessense to put Google to your bookmarks ? – Arioch 'The Jun 20 '13 at 09:39
  • Sorry I forgot to mention that my TImage is smaller than the actual dimensions of the Image, but, bummi solved that problem. – andyADD Jun 20 '13 at 19:14

1 Answers1

4

Replace LoadFromDevice with

MyBitmap.SetSize(370, 240); 
StretchBlt(MyBitmap.Canvas.Handle, //destination HDC
  0, 0, 370, 240, // destination size
  ScreenDC, //source HDC
  0, 0, Screen.Width, Screen.Height, // source size
  SrcCopy 
  );

Setting a smaller size on an existing Bitmap will just crop it.
Your intention is to scale the bitmap.

The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle, stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary. The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.

bummi
  • 27,123
  • 14
  • 62
  • 101
  • Oh that is perfect, I have two questions. 1. why replace loadfromdevice with this. 2. Link that will go more into detal with stretchblt so I know what the extra 0 mean. – andyADD Jun 20 '13 at 06:10