Page 1 of 1
Draw in a picturebox [solved]
Posted: Tuesday 12th January 2021 8:06pm
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.
Re: Draw in a picturebox
Posted: Tuesday 12th January 2021 10:18pm
by vuott
You could act on the "Image" Object (
theImage variable), using the specific Methods of the "
Paint " Class to draw lines.
Re: Draw in a picturebox
Posted: Wednesday 13th January 2021 8:18pm
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?
Re: Draw in a picturebox
Posted: Wednesday 13th January 2021 9:13pm
by vuott
Can you attach an essential sample code that produces that problem?
Re: Draw in a picturebox
Posted: Thursday 14th January 2021 6:36pm
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.
Re: Draw in a picturebox
Posted: Thursday 14th January 2021 11:27pm
by vuott
In the "Public Sub Form_Open ()" routine also set this command line:
ThePictureBox.Background = Color.Transparent
Re: Draw in a picturebox
Posted: Thursday 14th January 2021 11:57pm
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
Re: Draw in a picturebox
Posted: Saturday 16th January 2021 3:05pm
by Zeke99
Your the man Vuott. Works like a charm!
Re: Draw in a picturebox
Posted: Sunday 14th February 2021 2:10am
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.