Recent

Author Topic: [ SOLVED ] Zoomproblem with image in scrollbox  (Read 3685 times)

Jumbo

  • New Member
  • *
  • Posts: 29
[ SOLVED ] Zoomproblem with image in scrollbox
« on: March 17, 2019, 02:44:40 pm »
Hello programmers,

I have a simple form with 4 buttons a scrollbox and an image in the scrollbox.

On creationtime a picture is loaded in a bitmap and showed in the viewport aka scollbox-image. Perfekt!

When I click the Zoom+ button the image is enlarged *2, and the bitmap is copied to the image.
Now the problem is the image is bigger then what I expected it to be and a part right and below is never visable by scrolling.

When clicked on the Zoom- button after creation, the image is half the size but also a quarter size is visable in the upper-left corner.

When I click the scollbars there seems to be a double action. The handle is clicked right,  the handle goes right and then goes left a bit.
Also the image seems to be updated twice.

Clicking twice or more the same action, it becomes even more too big, or too small..
Zoom- would give even 3 or 4 pictures over each other.

So, Lines go well, coloring goes well. But problems with copying bitmap onto the Image in the Scrollbox.
b.t.w. Image1.Autosize / Proportional == False,  Strech == True(needed to view with the scollbars) 

I dont know what you need to help me out, but I have the zoom routines here and it is on Linux Mint.:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DoZoomInClick(Sender: TObject);
  2. var
  3.   W,H: Integer;
  4. begin
  5.   W := Image1.Width*2;
  6.   H := Image1.Height*2;
  7.   TgtRect := Rect(0,0,W,H);
  8.   // ScrRect == Size of Bitmap
  9.  
  10.   Image1.Height := H;
  11.   Image1.Width := W;
  12.  
  13.   // After the copy, the bitmap is sized in Image1.
  14.   // Scollbars can be used to view Image-parts outside viewport.
  15.   Image1.Canvas.CopyRect(TgtRect,Bitmap.Canvas,SrcRect);
  16.  
  17.   Debug.Text:=Format('Img W: %d, H: %d , Sbx W: %d, H: %d , Bm W: %d, H %d' ,
  18.   [ Image1.Width,Image1.Height,ScrollBox1.Width,ScrollBox1.Height,Bitmap.Width, Bitmap.Height]);
  19. end;
  20.  
  21. procedure TForm1.DoZoomOutClick(Sender: TObject);
  22. var
  23.   W,H: Integer;
  24. begin
  25.   W := Round(Image1.Width*0.5);
  26.   H := Round(Image1.Height*0.5);
  27.   TgtRect := Rect(0,0,W,H);
  28.   // ScrRect == Size of Bitmap
  29.  
  30.   Image1.Height := H;
  31.   Image1.Width := W;
  32.  
  33.   // After the copy, the bitmap is sized in Image1.
  34.   // Scollbars can be used to view Image-parts outside viewport.
  35.   Image1.Canvas.CopyRect(TgtRect,Bitmap.Canvas,SrcRect);
  36.  
  37.   Debug.Text:=Format('Img W: %d, H: %d , Sbx W: %d, H: %d , Bm W: %d, H %d' ,
  38.   [ Image1.Width,Image1.Height,ScrollBox1.Width,ScrollBox1.Height,Bitmap.Width, Bitmap.Height]);
  39. end;  
  40.  
« Last Edit: March 17, 2019, 06:07:11 pm by Jumbo »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Zoomproblem with image in scrollbox
« Reply #1 on: March 17, 2019, 04:46:08 pm »
b.t.w. Image1.Autosize / Proportional == False,  Strech == True(needed to view with the scollbars) 

It should be just the contrary: AutoSize = True, so that the TImage adjusts its size to the size of Picture; and Stretch = False, so that it doesn't try to adjust the Picture to the TImage dimensions.

There are also some other ... let's say, non-optimal things, like fiddiling directlry with the TImage.Canvas, etc. Rather than listing them all, I've prepared a small, quick and dirty example of image zooming. Find it in the attached zip.

HTH!
« Last Edit: March 17, 2019, 04:49:22 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.

Jumbo

  • New Member
  • *
  • Posts: 29
Re: Zoomproblem with image in scrollbox
« Reply #2 on: March 17, 2019, 04:59:54 pm »
Thanks for the reaction, but I am not sure if thats right.
I tried all posibilities with auto***  true and false.  But it did not work or became worse.

At last I played around with the TRect for the target window,  The Image in the Scrollbox.

I deleted it and now it works. I must say works magically.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DoZoomInClick(Sender: TObject);
  2. var
  3.   W,H: Integer;
  4. begin
  5.   W := Image1.Width*2;
  6.   H := Image1.Height*2;
  7.   //TgtRect := Rect(0,0,W,H);    <<<<<<<<<<<<
  8.   // ScrRect == Size of Bitmap
  9.  
  10.   Image1.Height := H;
  11.   Image1.Width := W;
  12.  
  13.   // After the copy, the bitmap is sized in Image1.
  14.   // Scollbars can be used to view Image-parts outside viewport.
  15.   Image1.Canvas.CopyRect(TgtRect,Bitmap.Canvas,SrcRect);
  16.   Image1.Canvas.Refresh;
  17.  
  18.   Debug.Text:=Format('Img W: %d, H: %d , Sbx W: %d, H: %d , Bm W: %d, H %d' ,
  19.   [ Image1.Width,Image1.Height,ScrollBox1.Width,ScrollBox1.Height,Bitmap.Width, Bitmap.Height]);
  20. end;
  21.  

As I expected the target has grown, so I have to tell the copyrect... But it is not needed.

It works now, But I don't understand why !

Double actions are gone, and the picture in picture also, when Zoom- is clicked.

If someone could explane what is going on, would be very nice. It is not funny to have working code, not knowing why..... ;)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Zoomproblem with image in scrollbox
« Reply #3 on: March 17, 2019, 05:20:30 pm »
It works because you're bypassing all the advanced features of TImage and using it simply as a Canvas in which to draw. If you prefer that, that's OK but then you would do better by using a TPaintBox, which is specifically designed for that kind of drawing.

Very basically, a TImage is just a container for a TPicture with some automagic features so that you can avoid doing by hand the most common presentation operations: centering the picture in its container, auosizing the contatiner, stretching the picture to fill the width/height (or both!) of the container, etc. But drawing operations should be done through the underlying TPicture, as my example does. Otherwise, it's almost always better to use a paint box.
« Last Edit: March 17, 2019, 05:22:36 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.

Jumbo

  • New Member
  • *
  • Posts: 29
Re: Zoomproblem with image in scrollbox
« Reply #4 on: March 17, 2019, 06:06:54 pm »
Thanks Lucamar. 

I tried with TPaintbox, but in my case the TImage works fine. With TPaint my program needs to change a bit more...
So I decided to leave it this way.

Jumbo

  • New Member
  • *
  • Posts: 29
Re: [ SOLVED ] Zoomproblem with image in scrollbox
« Reply #5 on: March 18, 2019, 08:13:24 am »
Update at last....

Lucamar was right. A TPaintBox is the better choice.

When loading another picture at runtime with a different aspect ratio was neer imposible to get right on the screen.

So at last changed to TPaintBox and added some  "restore the image"  procedure to the OnPaint Event.
Resize the Paintbox canvas as needed.... Scrolling works well in the ScrollBox.
The CopyRect was also needed again. easy.

Now the aspect ratio is always right and the picture can be resized and repainted. No problems.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [ SOLVED ] Zoomproblem with image in scrollbox
« Reply #6 on: March 18, 2019, 03:26:02 pm »
When loading another picture at runtime with a different aspect ratio was neer imposible to get right on the screen.

That shouldn't happen ... unless your fiddling with the TImage canvas renders it unable to adapt itself to the new image. Did you at least look to my previous code? It works!

Anyway, with your preferred way of doing things a TPaintBox is almost certainly better.

Glad it helped!
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.

 

TinyPortal © 2005-2018