Recent

Author Topic: Drawing a Rectangle on an Image  (Read 1643 times)

Cran0g

  • New Member
  • *
  • Posts: 28
Drawing a Rectangle on an Image
« on: July 21, 2024, 06:33:47 pm »
Hi Folks,

Fairly new to Lazarus.

I have a graphics problem that I just can't seem to sort - I have checked the forum and nothing seems to answer the question or I simply cannot get it to work.

Here's what I'm trying to do:

I have a scrollbox which contains a large image of a place in Scotland (where I come from).  When compiled, this works just fine and scrolls around as required.

I want to draw a rectangle on this image while it's in the scrollbox.  I realise that this would have to be in a form "repaint" handler as it would not be persistent but I've had no success in getting this rectangle to appear.

Can anyone help me here or point me to something I can use?  I'm using v3.4 of Lazarus with FPC 3.2.2 in a Windows 64 environment.

Many thanks

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Drawing a Rectangle on an Image
« Reply #1 on: July 21, 2024, 07:10:04 pm »
There are a couple of things you can do.

In the OnPaint cycle where the image I assume is being updated, you can draw the box then on top of that.

I don't know how you are drawing the image; I assume you are using the OnPaint event, but the image should be cached away somewhere in a ImageList, Timage or A Tbitmap.

 The other way is to Create a TGraphicControl which can be semitransparent and be on top. The Paint cycle for that control will allow you to draw the box and also it will receive you mouse input so you can do things like move the box around etc.


Come back if that is interesting to you.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: Drawing a Rectangle on an Image
« Reply #2 on: July 21, 2024, 07:23:16 pm »
The most natural way to draw an image it to use a TImage component which does all the painting, scaling and centering (if needed) etc for you. You only assign the picture to the Picture property of the TImage. In order paint a rectangle onto the image use the OnPaint event of the TImage.

See attached demo.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 270
Re: Drawing a Rectangle on an Image
« Reply #3 on: July 22, 2024, 10:52:40 am »
Interesting...
How to draw the red line around the image ?
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: Drawing a Rectangle on an Image
« Reply #4 on: July 22, 2024, 02:00:05 pm »
Code: Pascal  [Select][+][-]
  1.   Image1.Canvas.Rectangle(0, 0, Image1.Picture.Width, Image1.Picture.Height);

jcmontherock

  • Sr. Member
  • ****
  • Posts: 270
Re: Drawing a Rectangle on an Image
« Reply #5 on: July 22, 2024, 04:29:33 pm »
Thanks a lot.
Windows 11 UTF8-64 - Lazarus 4RC1-64 - FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Drawing a Rectangle on an Image
« Reply #6 on: July 22, 2024, 04:37:34 pm »
I just posted some code here TGripper, it may give you an idea of what to do with it.

Its early starting code but I can give you some ideas to enhance it.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018