[Gambas-user] drawing rectangles on a picture.

Tobias Boege taboege at gmail.com
Sun Oct 18 14:30:38 CEST 2020


On Sun, 18 Oct 2020, KKing wrote:
> So I want to load a png, then draw some rectangles on it and resave it.
> 
> Any pointers, after reading and searching I'm just confused by Draw, Paint,
> DrawingArea, PictureBox etc.
> 

Draw is the old and meanwhile deprecated interface. Between Draw and Paint,
you should always go for Paint. You do not need a DrawingArea or a Picture-
Box to draw on a picture and save it to disk. Those two are for displaying
pictures on your form. You .Load the picture, paint on it and .Save it.

If you have never used Paint before, it can take some time to get used to.
First, you have to establish a painting context on your picture by wrapping
Paint.Begin and Paint.End calls around your painting. Then, the painting
model consists of adding "paths" and either stroking or filling those paths.

To draw a rectangle on a picture hPic, you do

  Paint.Begin(hPic)
    ' Define an invisible path for drawing a rectangle at
    ' X=10, Y=20 with Width=50, Height=100.
    Paint.Rectangle(10, 20, 50, 100)

    ' Stroke the path in red and do not delete the path afterwards,
    ' we'll use it again.
    Paint.Brush = Paint.Color(&H00ff0000&) ' ARGB color (fully opaque red)
    Paint.Stroke(True)

    ' Fill the rectangle in semitransparent blue (reuses the previous path).
    Paint.Brush = Paint.Color(&H800000ff&)
    Paint.Fill() ' path is automatically deleted
  Paint.End()

> Also why does
>     PictureBox1.Picture = Picture.Load("/media/user01/Test01.png")
> work but
>   myPicture = New Picture
>   myPicture.Load("/media/user01/Test01.png")
>   PictureBox1.Picture = myPicture
> does not ?
> 

Picture.Load is a static method of the Picture class. It loads a file and
returns a new picture. You seem to be thinking that myPicture.Load would
fill myPicture, but it doesn't. You load the new picture, throw it away
and assign Null to the PictureBox with the second snippet.

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list