Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Using bitmap As New Bitmap(CameraView.Width, CameraView.Height)
      Using g As Graphics = Graphics.FromImage(bitmap)
           ' Get the paramters to call g.CopyFromScreen and get the image
           Dim rectanglePanelVideoPreview As Rectangle = CameraView.Bounds
           Dim sourcePoints As System.Drawing.Point = CameraView.PointToScreen(New System.Drawing.Point(CameraView.ClientRectangle.X, CameraView.ClientRectangle.Y))
            g.CopyFromScreen(sourcePoints, System.Drawing.Point.Empty, rectanglePanelVideoPreview.Size)
       End Using
End Using

i want to use backgroundworker to do that code, but got error because use PointToScreen.
Please help me re-code with cross-thread. Thx
Posted

1 solution

Actual drawing must be done in the UIThread. This is the main thread running the application and handling the user interface. You need to tell the UI Thread to invoke the method so it is allowed to do screen updates. In the backgroundworker you would only do all the work prior to the actual drawing. When the work is done you then invoke a method that updates the UI. That method should check if an invoke is required, meaning that is another thread is calling the method, it will automatically invoke itself to be executed on the UI thread.

VB
If Me.InvokeRequired Then
    Me.Invoke(New MethodInvoker(AddressOf AccessControl))
Else


Check this link for more info:
http://msdn.microsoft.com/en-us/library/ms171728%28v=vs.85%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1[^]

Good luck!
 
Share this answer
 
Comments
AlbertusChW 28-Nov-14 11:38am    
sry can you help me recode it? i dont really understand about using invoke for this PointToScreen
thx
E.F. Nijboer 28-Nov-14 13:32pm    
The code you posted with the question isn't something to handle in a background worker because it interacts with the UI. To call it from a background worker just add the code into a method of the form. Add the InvokeRequired around the code and invoke if needed. There are suitable examples on the msdn page. Have a look at the SetText example. It only has a single parameter but you can easily do the same and add more parameters as needed. Just give it a try ;-)
AlbertusChW 29-Nov-14 3:31am    
yes i know, it cannot do in background, and i need some invoke do for me. I can invoke for SetText, but i cant do for PointToScreen, please re-code it. Thx
E.F. Nijboer 29-Nov-14 8:29am    
I'm sorry but you'll have to try yourself first. If you run into any problems you can post a new question with the code of what you have tried and ask for help. I can't just recode it for you, it's not how programming works.
AlbertusChW 30-Nov-14 2:44am    
i has try it, but it doesnt work like before

this is my code

Using bitmap As New Bitmap(CameraView.Width, CameraView.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
' Get the paramters to call g.CopyFromScreen and get the image
Dim rectanglePanelVideoPreview As Rectangle = CameraView.Bounds
Dim sourcePoints As System.Drawing.Point = CameraView.Invoke(Sub() CameraView.PointToScreen(New System.Drawing.Point(CameraView.ClientRectangle.X, CameraView.ClientRectangle.Y)))
g.CopyFromScreen(sourcePoints, System.Drawing.Point.Empty, rectanglePanelVideoPreview.Size)
End Using
End Using

am i wrong? yes of course, that is why i am asking here..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900