Draw in a picturebox [solved]

Post your Gambas programming questions here.
Post Reply
Zeke99
Posts: 10
Joined: Tuesday 12th January 2021 7:44pm

Draw in a picturebox [solved]

Post by Zeke99 »

I have a videofeed from a usb-microscope in a picturebox using mediaplayer. Works great (code copied from Cedron)

Code: Select all

Private myPlayer As New MediaPlayer As "Player"
'=============================================================================
Public Sub Form_Open()
         myPlayer.URL = "v4l2:///dev/video0"
        myPlayer.Play()    
End
'=============================================================================
Public Sub Form_Close()
        myPlayer.Stop()
End
'=============================================================================
Public Sub TheButton_Click()
        Dim theImage As Image = myPlayer.Video.Image
        theImage.Save(Application.Path &/ Format(Now, "yyyymmdd_hhnnss") & ".jpg"
        ThePictureBox.Image = theImage
End
Is there a way to draw a 2 lines (a crosshair) in the Picturebox or put a crosshair in a transparent Drawingarea on top of the Picturebox?
None of my attempts have been successful so far.
Last edited by Zeke99 on Sunday 19th November 2023 6:21pm, edited 1 time in total.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott »

You could act on the "Image" Object (theImage variable), using the specific Methods of the " Paint " Class to draw lines.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Zeke99
Posts: 10
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 »

Thank's a lot vuott. It worked to use paint directly on theImage.

Code: Select all

 Dim theImage As Image = myPlayer.Video.Image
         
        theImage.Save(Application.Path &/ Format(Now, "yyyymmdd_hhnnss") & ".jpg" 
        ThePictureBox.Image = theImage
        
        Paint.Begin(theImage)
        Paint.LineWidth = 1
        Paint.Rectangle(10, 10, 100, 100)
        Paint.Stroke
        Paint.End
The problem is that I was thinking wrong. theImage is just one image but I want to get a haircross over the live videofeed. It works in opencv and python using cv2.line(...) but as I understand Gambas don't support opencv (correct me if I'm wrong). Gambas is so much nicer to work in so I would rather solve it here. I tried to put FMain form over the mediaplayer form and made it transparent. So far so good but as soon as I try to paint something in an picturebox in the Fmain form the whole picturebox went grey and not transparent. Is there some way to draw/paint a line in some control but leave the background transparent?
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott »

Can you attach an essential sample code that produces that problem?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Zeke99
Posts: 10
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 »

Here comes a sample code:

Code: Select all

Private myPlayer As New MediaPlayer As "Player"
 
'=============================================================================
Public Sub Form_Open()
 
        '--- Opens a mediaplayer window with live video stream ---
        myPlayer.URL = "v4l2:///dev/video0"
        myPlayer.Play()
        
        ' --- Make the FMain window transparent so you can see the
        ' --- mediaplayer window behind.
        FMain.Transparent = True
End

'=============================================================================
Public Sub Button2_Click()

ThePictureBox.Cached = True ' -- Get "Cannot paint outside of Draw event handler i FMain:22" without cached=True

Paint.Begin(ThePictureBox)
Paint.LineWidth = 1
Paint.Rectangle(10, 10, 100, 100)
Paint.Stroke
Paint.End
End

'=============================================================================
Public Sub Form_Close()
 
        myPlayer.Stop()
 
End
'=============================================================================
The whole FMain is transparent including the picturebox in FMain. As soon as I hit the Button2 the picturebox turns grey and draw a rectangle in the grey picturebox.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott »

In the "Public Sub Form_Open ()" routine also set this command line:
ThePictureBox.Background = Color.Transparent
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott »

However, I would use a "DrawingArea":
Private myPlayer As New MediaPlayer As "Player"
 
'=============================================================================
Public Sub Form_Open()
 
'--- Opens a mediaplayer window with live video stream ---
  myPlayer.URL = "v4l2:///dev/video0"
  myPlayer.Play()

  FMain.Transparent = True
        
' It locks the Events of the "DrawingArea":
  Object.Lock(DrawingArea1)
        
End


Public Sub Button1_Click()

' It unlocks the Events of the "DrawingArea":
   Object.Unlock(DrawingArea1)

' It stimulates the redraw of the DrawingArea:
   DrawingArea1.Refresh

End


Public Sub DrawingArea1_Draw()

' It draws a simple crosshair:
  With Paint
    .Brush = .Color(Color.Red)
    .LineWidth = 2.0
    .MoveTo(DrawingArea1.W * 0.25, DrawingArea1.H / 2)
    .LineTo(DrawingArea1.W * 0.75, DrawingArea1.H / 2)
    .MoveTo(DrawingArea1.W / 2, DrawingArea1.H * 0.25)
    .LineTo(DrawingArea1.W / 2, DrawingArea1.H * 0.75)
    .Stroke
    .End
  End With

End


Public Sub Form_Close()
 
  myPlayer.Stop()
  myPlayer.Close()
 
End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Zeke99
Posts: 10
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 »

Your the man Vuott. Works like a charm!
User avatar
Technopeasant
Posts: 140
Joined: Saturday 13th July 2019 6:50pm
Location: Stony Plain, Alberta, Canada
Contact:

Re: Draw in a picturebox

Post by Technopeasant »

Or... if I understand correctly... you could just have the cross-hair as its own picture box on top of the feed?

I use picture boxes for any dynamic elements, e.g. ones that move or are re-drawn frequently, so that I do not need to redraw the background elements each time.

If you are getting a new frame all the time though I guess that is not a really big consideration.
Technical director,
Piga Software
http://icculus.org/piga/
Post Reply