D
Dave
Hey all,
I have a problem and I can't find anything on the net to really address
it. We have a program at work that has lots of numbers on the front of
the form because it is a control program for a hydro plant. My boss
would like me to create a web page so our control center can see all of
the numbers but not have access to the actual form. Some of these
numbers are gathered every second, others every five seconds and still
others every minute or ten minutes.
I was thinking the easiest thing to do would be to capture the form's
image every five seconds or so and write the image to disk and then use
an image control on a web page to display it. I have found code
samples to capture a form's image but I'm having a problem with the
function to get the form's image. The code below is called from a
timer that goes off every 5 seconds:
<code>
Dim bm As Bitmap
Try
' Get this form's Graphics object.
Dim myGraphics As Graphics = Me.CreateGraphics
' Make a Bitmap to hold the image.
bm = New Bitmap(Me.Width, Me.Height, myGraphics)
Dim bitmapGraphics As Graphics = Graphics.FromImage(bm)
Dim bitmapHandle As IntPtr = bitmapGraphics.GetHdc
' Get the form's hDC. We must do this after
' creating the new Bitmap, which uses myGraphics.
Dim myHandle As IntPtr = Me.CreateGraphics.GetHdc()
' BitBlt the form's image onto the Bitmap.
BitBlt(bitmapHandle, 0, 0, Me.ClientSize.Width,
Me.ClientSize.Height, _
myHandle, 0, 0, SRCCOPY)
bitmapGraphics.ReleaseHdc(bitmapHandle)
bitmapGraphics.Dispose()
myGraphics.Dispose()
Catch ex As Exception
GiveError(ex.ToString, "Error Getting Form Image")
End Try
' Return the result.
Return bm
</code>
The message I get is:
"An unhandled exception of type 'System.InvalidOperationException'
occurred in your System.Drawing.dll. Additional information: The
object is currently in use elsewhere."
I know this is occurring on the second call of the function because I
have disabled the timer after the first call and I don't get the error.
I have also tried increasing the timer's Interval to 10 seconds but I
still get the same error.
Has anyone run across this before? All of the posts I have seen on the
web have talked about multiple threads. I know the timer is supposed
to be on its own thread but I don't know how to get around that.
Another workaround I have thought about, instead of capturing the
form's image, is to publish my own performance counters. However,
there are about 30 values to keep track of and that doesn't seem to be
a good solution.
If anyone has suggestions, I'm all ears.
Thanks,
Dave
I have a problem and I can't find anything on the net to really address
it. We have a program at work that has lots of numbers on the front of
the form because it is a control program for a hydro plant. My boss
would like me to create a web page so our control center can see all of
the numbers but not have access to the actual form. Some of these
numbers are gathered every second, others every five seconds and still
others every minute or ten minutes.
I was thinking the easiest thing to do would be to capture the form's
image every five seconds or so and write the image to disk and then use
an image control on a web page to display it. I have found code
samples to capture a form's image but I'm having a problem with the
function to get the form's image. The code below is called from a
timer that goes off every 5 seconds:
<code>
Dim bm As Bitmap
Try
' Get this form's Graphics object.
Dim myGraphics As Graphics = Me.CreateGraphics
' Make a Bitmap to hold the image.
bm = New Bitmap(Me.Width, Me.Height, myGraphics)
Dim bitmapGraphics As Graphics = Graphics.FromImage(bm)
Dim bitmapHandle As IntPtr = bitmapGraphics.GetHdc
' Get the form's hDC. We must do this after
' creating the new Bitmap, which uses myGraphics.
Dim myHandle As IntPtr = Me.CreateGraphics.GetHdc()
' BitBlt the form's image onto the Bitmap.
BitBlt(bitmapHandle, 0, 0, Me.ClientSize.Width,
Me.ClientSize.Height, _
myHandle, 0, 0, SRCCOPY)
bitmapGraphics.ReleaseHdc(bitmapHandle)
bitmapGraphics.Dispose()
myGraphics.Dispose()
Catch ex As Exception
GiveError(ex.ToString, "Error Getting Form Image")
End Try
' Return the result.
Return bm
</code>
The message I get is:
"An unhandled exception of type 'System.InvalidOperationException'
occurred in your System.Drawing.dll. Additional information: The
object is currently in use elsewhere."
I know this is occurring on the second call of the function because I
have disabled the timer after the first call and I don't get the error.
I have also tried increasing the timer's Interval to 10 seconds but I
still get the same error.
Has anyone run across this before? All of the posts I have seen on the
web have talked about multiple threads. I know the timer is supposed
to be on its own thread but I don't know how to get around that.
Another workaround I have thought about, instead of capturing the
form's image, is to publish my own performance counters. However,
there are about 30 values to keep track of and that doesn't seem to be
a good solution.
If anyone has suggestions, I'm all ears.
Thanks,
Dave