Recent

Author Topic: The scaled image opens in Image2 instead of Image1  (Read 3355 times)

kolbjoern

  • New Member
  • *
  • Posts: 10
The scaled image opens in Image2 instead of Image1
« on: December 16, 2018, 06:22:23 pm »
I have a form with two TImage components, Image1 and Image2 and a button to start the program.
The procedure shown in the attachment and below.
When running I expected the image to be opened in Image1 but the scaled image opens in Image2. Why?
The unscaled image is shown in Image1.

procedure scaleImage;
var
  Img: TImage;
begin
  OpenDialog1.Execute;
  if (OpenDialog1.Files.Count = 1) and (FileExists(OpenDialog1.FileName)) then
  begin
    Image2.Picture.LoadFromFile(OpenDialog1.FileName);
    Img := Image2;
    Img.Proportional:= True;
    Img.Width := 400;
    Img.Height:= 267;
    Image1.Picture := Img.Picture;
  end;
end;
Kolbjørn

« Last Edit: December 16, 2018, 06:36:45 pm by kolbjoern »

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: The scaled image opens in Image2 instead of Image1
« Reply #1 on: December 16, 2018, 06:54:38 pm »
You (unnecessarily) assign the Image2 to a variable img - this is just am abbreviation for typing. So, whenever you write img you mean Image2. You set img.Proportional to true - because img=Image2 this sets Images2.Proportional to true. This scales the raw image to the size of image2 (I thought that it is required also to set Stretch to true, but this seems to be wrong).

Then you set Image1.Picture := img.Picture. This does not copy the scaled image, it copies the raw data which are in Img = Image2.

Therefore, you have the unscaled image im Image1 and the scaled image in Image2.

kolbjoern

  • New Member
  • *
  • Posts: 10
Re: The scaled image opens in Image2 instead of Image1
« Reply #2 on: December 16, 2018, 08:03:07 pm »
Thank you wp.
I thought the contents of Image2 was copied into img and then I could manipulate img independently of the content of Image2 just like b := 3;a := b + 1 with the result a = 4; b = 3. I am new to image handling and obviously have lots to learn :-).
Kolbjørn

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: The scaled image opens in Image2 instead of Image1
« Reply #3 on: December 16, 2018, 10:43:31 pm »
Try something like this (untested) code:
Code: Pascal  [Select][+][-]
  1. procedure scaleImage;
  2. begin
  3.   OpenDialog1.Execute;
  4.   if (OpenDialog1.Files.Count = 1) and (FileExists(OpenDialog1.FileName)) then
  5.   begin
  6.     Image2.Picture.LoadFromFile(OpenDialog1.FileName);
  7.     Image1.Proportional:= True;
  8.     Image1.Width := 400;
  9.     Image1.Height:= 267;
  10.     Image1.Picture.Assign(Image2.Picture);
  11.   end;
  12. end;

It's not only a question a question of image handling, it's a question of object handling. Using ":=" simply copies the reference so the two variables point to the same object. The Assign method, however, should copy the fields from one object to another if either one know  how to do it.
« Last Edit: December 16, 2018, 10:48:45 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

kolbjoern

  • New Member
  • *
  • Posts: 10
Re: The scaled image opens in Image2 instead of Image1
« Reply #4 on: December 17, 2018, 04:57:38 pm »
I had simplified my program example to show what I was asking for. I did not think of it as object handling for some reasons. So thank you for reminding med. And yes, your code works.
The reason for my question was that I would show a miniature of the image, adding some data to the image and then save it i full size, not the scaled copy. Thanks to you both I now have a better understanding of how to do this.
Kobjørn

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: The scaled image opens in Image2 instead of Image1
« Reply #5 on: December 17, 2018, 05:46:03 pm »
The reason for my question was that I would show a miniature of the image, adding some data to the image and then save it i full size, not the scaled copy.

Note that you can get by with just one TImage. When you set Stretch, Proportional, etc. the only thing that changes is how Picture is drawn, not the underlying picture data.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: The scaled image opens in Image2 instead of Image1
« Reply #6 on: December 17, 2018, 05:54:53 pm »
I guess you'll want scaling done with Picture.Canvas.CopyRect(), if you want the saved file to be different size than original.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: The scaled image opens in Image2 instead of Image1
« Reply #7 on: December 17, 2018, 06:14:13 pm »
I guess you'll want scaling done with Picture.Canvas.CopyRect(), if you want the saved file to be different size than original.

He said he wants to save the full-sized image. Even if it were not so, it would be better to use StretchDraw for scaling--that is, besides the advanced resamplers in the FCL.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

kolbjoern

  • New Member
  • *
  • Posts: 10
Re: The scaled image opens in Image2 instead of Image1
« Reply #8 on: December 18, 2018, 08:46:27 pm »
I have experimented with your suggestions and yes you are right of course. Thank you a lot for using your time on me.
I have not used Pascal for many years and found it was the time for refreshing my knowledge about this interesting language a bit. Perhaps image handling was a bit too difficult for me as a start. :)
Kolbjørn

 

TinyPortal © 2005-2018