Draw in a picturebox

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

Draw in a picturebox

Post by Zeke99 » Tuesday 12th January 2021 8:06pm

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.

vuott
Posts: 69
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott » Tuesday 12th January 2021 10:18pm

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

Zeke99
Posts: 4
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 » Wednesday 13th January 2021 8:18pm

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: 69
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott » Wednesday 13th January 2021 9:13pm

Can you attach an essential sample code that produces that problem?

Zeke99
Posts: 4
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 » Thursday 14th January 2021 6:36pm

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: 69
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott » Thursday 14th January 2021 11:27pm

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

vuott
Posts: 69
Joined: Wednesday 05th April 2017 6:07pm
Location: European Union

Re: Draw in a picturebox

Post by vuott » Thursday 14th January 2021 11:57pm

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

Zeke99
Posts: 4
Joined: Tuesday 12th January 2021 7:44pm

Re: Draw in a picturebox

Post by Zeke99 » Saturday 16th January 2021 3:05pm

Your the man Vuott. Works like a charm!

User avatar
Technopeasant
Posts: 59
Joined: Saturday 13th July 2019 6:50pm

Re: Draw in a picturebox

Post by Technopeasant » Sunday 14th February 2021 2:10am

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