Draw in a picturebox [solved]

Post

Posted
Rating:
#1 (In Topic #552)
Trainee
I have a videofeed from a usb-microscope in a picturebox using mediaplayer. Works great (code copied from Cedron)

Code

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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
You could act on the "Image" Object (theImage variable), using the specific Methods of the " Paint " Class to draw lines.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
Thank's a lot vuott. It worked to use paint directly on theImage.

Code

 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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’
Can you attach an essential sample code that produces that problem?

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
Here comes a sample code:

Code

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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’
In the "Public Sub Form_Open ()" routine also set this command line:

   

Code (gambas)

  1. ThePictureBox.Background = Color.Transparent

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
vuott is in the usergroup ‘Regular’
However, I would use a "DrawingArea":

Code (gambas)

  1. Private myPlayer As New MediaPlayer As "Player"
  2.  
  3. '=============================================================================
  4. Public Sub Form_Open()
  5.  
  6. '--- Opens a mediaplayer window with live video stream ---
  7.   myPlayer.URL = "v4l2:///dev/video0"
  8.   myPlayer.Play()
  9.  
  10.   FMain.Transparent = True
  11.        
  12. ' It locks the Events of the "DrawingArea":
  13.   Object.Lock(DrawingArea1)
  14.        
  15.  
  16.  
  17. Public Sub Button1_Click()
  18.  
  19. ' It unlocks the Events of the "DrawingArea":
  20.    Object.Unlock(DrawingArea1)
  21.  
  22. ' It stimulates the redraw of the DrawingArea:
  23.    DrawingArea1.Refresh
  24.  
  25.  
  26.  
  27. Public Sub DrawingArea1_Draw()
  28.  
  29. ' It draws a simple crosshair:
  30.   With Paint
  31.     .Brush = .Color(Color.Red)
  32.     .LineWidth = 2.0
  33.     .MoveTo(DrawingArea1.W * 0.25, DrawingArea1.H / 2)
  34.     .LineTo(DrawingArea1.W * 0.75, DrawingArea1.H / 2)
  35.     .MoveTo(DrawingArea1.W / 2, DrawingArea1.H * 0.25)
  36.     .LineTo(DrawingArea1.W / 2, DrawingArea1.H * 0.75)
  37.     .Stroke
  38.     .End
  39.  
  40.  
  41.  
  42. Public Sub Form_Close()
  43.  
  44.   myPlayer.Stop()
  45.   myPlayer.Close()
  46.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
Your the man Vuott. Works like a charm!
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
Technopeasant is in the usergroup ‘Regular’
 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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.