Recent

Author Topic: [SOLVED]Bitmap as a parameter  (Read 2416 times)

Cliff48

  • Jr. Member
  • **
  • Posts: 51
[SOLVED]Bitmap as a parameter
« on: June 14, 2015, 02:24:19 am »
I have a function that takes an image and makes a Thumbnail image;

So I want to call:

CreateThumbnail(fname,  mybmp, thumbwidth, thumbheight);

but I cannot work out how my CreateThumbnail parameters should be defined:

Function CreateThumbnail(fname: String; var bmp: Tbitmap; tw, th: LongInt): Boolean;

I try using type of Tbitmap, BITMAP but get  compile errors.  I cant find an acceptable combination that FPC will accept.  I want this as reusable code, so I dont want to code the bitmap name as a global variable.  ( I already have that working, but its hardly reusable in that form.

Appreciate any help.

« Last Edit: June 14, 2015, 04:03:49 am by Cliff48 »
Laz Ver 1.4.0  FPC 2.6.4 SVN 48774
Win 7 Ultimate SP1
Intel I7-2600 CPU @  3.40GHZ 8GB Ram 64bit OS

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bitmap as a parameter
« Reply #1 on: June 14, 2015, 03:18:39 am »
Code: [Select]
function CreateThumbnail(AName: string; var ABitmap: TBitmap; TW, TH: LongInt): Boolean;
var
  ThumbNail: TBitmap;
  R: TRect;
begin
  Result := False;
  ThumbNail := TBitmap.Create;
  try
    ThumbNail.Width := TW;
    ThumbNail.Height := TH;
    ThumbNail.Canvas.StretchDraw(Rect(0,0,tw, th), ABitmap);
    ABitmap.Assign(ThumbNail);
    Result := True;
  finally
    ThumbNail.Free;
  end;
end; 

usage:

Code: [Select]
procedure TForm1.Button1Click(Sender :TObject);
var
  bmp: TBitmap;
begin
  bmp := Image1.Picture.Bitmap;
  CreateThumbnail('name', bmp, 100, 100);
  Image1.Picture.Bitmap := bmp;
end;

In your uses clause, put Windows before Classes unit.
« Last Edit: June 14, 2015, 03:22:59 am by typo »

Basile B.

  • Guest
Re: Bitmap as a parameter
« Reply #2 on: June 14, 2015, 03:36:33 am »
var is even not necessary since TBitmap is a reference type (it's already a pointer).
It's even better to remove it because with var the type of the parameter must be formally the same as the parameter type: sub class not allowed. Without var you can pass some TBitmap descendant.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bitmap as a parameter
« Reply #3 on: June 14, 2015, 03:50:23 am »
You could use Ratio: double instead of TW and TH.

Cliff48

  • Jr. Member
  • **
  • Posts: 51
Re: Bitmap as a parameter
« Reply #4 on: June 14, 2015, 04:03:15 am »
Thank you both for your very helpful answers, especially as to why I am having the problems.

@typo.  I am using dw, dh as I am scaling to keep the same aspect ratio and will be used as icons on stringgrid. 

Like most people I seemed to have collected icons, glyphs, images, etc from all over the net, and just stored them where I was going to be using them at the time.  I figured now was a good time to try to organise stuff as I am making a a more serious attempt to learn and use FPC/Lazarus capabilities in a more organised way.  So look forward to  more questions on basic stuff like this while I learn:)

Thanks again
Laz Ver 1.4.0  FPC 2.6.4 SVN 48774
Win 7 Ultimate SP1
Intel I7-2600 CPU @  3.40GHZ 8GB Ram 64bit OS

 

TinyPortal © 2005-2018