Recent

Author Topic: Main form takes on dimensions of loaded bitmap  (Read 1721 times)

jave

  • New member
  • *
  • Posts: 7
Main form takes on dimensions of loaded bitmap
« on: October 16, 2018, 12:41:46 pm »
Hi all,

Me again. lol

I've made some changes to my code so that when a bitmap file is loaded, the image displays on the 2nd form.  This works fine, however when I click on a button to process the bitmap image, my main form takes on the dimensions of the loaded bitmap.  What am I doing wrong?

Code:
Code: Pascal  [Select]
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. begin
  3.   BitMap2 := TBitMap.create;
  4.   BitMap2.Assign(BitMap1);
  5.   width := BitMap2.Width;
  6.   height := BitMap2.Height;
  7.   Label3.Caption := IntToStr(width);
  8.   Label4.Caption := IntToStr(height);
  9.   for px:= 0 to width do begin
  10.       for py:= 0 to height do begin
  11.         // get colour
  12.         colour1 := BitMap2.Canvas.Pixels[px,py];
  13.         colour2 := BitMap2.Canvas.Pixels[px+1,py];
  14.         if (colour1 <> colour2) then begin // if pixels do not match
  15.            BitMap2.Canvas.Pixels[px,py] := clRed;
  16.            // colour1 := BitMap1.Canvas.Pixels[px,py];
  17.         end
  18.       end;
  19.   end;
  20.   for py:= 0 to height do begin
  21.       for px:= 0 to width do begin
  22.         // get colour
  23.         colour1 := BitMap2.Canvas.Pixels[px,py];
  24.         colour2 := BitMap2.Canvas.Pixels[px,py+1];
  25.         if (colour1 <> colour2) then begin // if pixels do not match
  26.            BitMap2.Canvas.Pixels[px,py] := clRed;
  27.            // colour1 := BitMap1.Canvas.Pixels[px,py];
  28.         end
  29.       end;
  30.   end;
  31.   Label6.Caption := 'Image processed.';
  32.   Form3.Image1.Canvas.Draw(0,0,BitMap2);
  33.   Form3.Image1.Stretch := True ;
  34. end;
  35.  

I'm pretty sure I don't need the 2nd bitmap but added it as I was trying a few different things.  Thanks!

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3633
  • I like bugs.
Re: Main form takes on dimensions of loaded bitmap
« Reply #1 on: October 16, 2018, 12:53:22 pm »
You explicitly set width and height of TForm1 at lines 5 and 6 of your listing.

jave

  • New member
  • *
  • Posts: 7
Re: Main form takes on dimensions of loaded bitmap
« Reply #2 on: October 17, 2018, 08:01:04 am »
Okay, so how can I obtain the width and height of the loaded bitmap image?  :)

wp

  • Hero Member
  • *****
  • Posts: 6231
Re: Main form takes on dimensions of loaded bitmap
« Reply #3 on: October 17, 2018, 09:50:55 am »
Declare width and height as local variables. But you must be aware that these names are already defined by the form (that's the reason why the size of the form changes in your code), fpc in mode objfpc rejects local variables with this ambiguity. Therefore, name them "w" and "h", or "lWidth" and "lHeight", "l" for "local":
Code: Pascal  [Select]
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. var
  3.   lWidth, lHeight: Integer;   // <--- NEW
  4. begin
  5.   BitMap2 := TBitMap.create;
  6.   BitMap2.Assign(BitMap1);
  7.   lWidth := BitMap2.Width;
  8.   lHeight := BitMap2.Height;
  9.   Label3.Caption := IntToStr(lWidth);
  10.   Label4.Caption := IntToStr(lHeight);
  11.   for px:= 0 to lWidth do begin
  12.     ...
« Last Edit: October 17, 2018, 11:15:52 am by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jave

  • New member
  • *
  • Posts: 7
Re: Main form takes on dimensions of loaded bitmap
« Reply #4 on: October 17, 2018, 10:19:55 am »
Ah okay.  Many thanks!!