473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

getting a form's image every so often

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

Apr 24 '06 #1
3 1743
This is just a guess, but I think you need to do a ReleaseHdc on myHandle. I
have a similar function, it also returns a bitmap, and I have two win32 api
calls to ReleaseDC, one for the source and one for the destination.
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


Apr 24 '06 #2
AMercer,

Thanks for the tip but that didn't help. At first it ran for almost a
full minute and then errored out with the same error. After I
restarted the program it gave me the error almost immediately each time
I tried the program.

Dave

Apr 25 '06 #3
I have two suggestions.

1. If you don't already know, find out which statement generated the
exception. Run the code without the try block, let it fail, and let the
debugger tell you where the failure occurs.

2. Sometimes the inner exception gives additional info. Try reporting
ExceptionString(ex) instead of ex.ToString -

Public Function ExceptionString(ByVal MyException As Exception) As String
' return a multiline exception description
Dim t as String = ""
Try
Dim s As String = "Exception"
Dim e As Exception = MyException
While (Not e Is Nothing) And (Len(t) < 10000)
t &= s & ": " & e.ToString
s = "From"
e = e.InnerException
End While
Catch ex As Exception
End Try
Return t
End Function

Apr 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
6
by: Wayne Wengert | last post by:
Using ASP 3 on an ISP account (I don't have access to the root of the server). I have a page that does some intake processing and I'd like to have it run every 5 minutes. Right now, the only way I...
2
by: tom | last post by:
Hello, does anyone know if and how it is possible that when you use an image as backgroundimage of a form, this image adjusts itself when the form resizes. Sth. like 'StretchImage' for a...
0
by: JF Turcotte | last post by:
Hi I'm unsuccessfully trying to print a form's image under VB.NET. To print under .NET is a real pain in the , I find it to be complex, lenghty, confusing, upsetting and ultimately not to be...
1
by: news.tesco.net | last post by:
Dear all, I have two questions for all you experts out there...! 1. Is there a way of getting the system image list using the .NET framework? 2. I can get the system image list using...
5
by: Stan Shankman | last post by:
C# -- Visual Studio.Net – Windows Application Greetings all, How do I copy a Form’s image to a bitmap? – And do so before it gets to the screen. I haven’t been able to find anyone that knows...
2
by: nishi.hirve | last post by:
Hello, I am writing 1 client-server application in which I have written my server in C on linux system and client in C#. My server is sending image bytes of that image size to the client....
1
hariharanmca
by: hariharanmca | last post by:
How to Load form back image Dynamically in VB 6
4
by: Jerry West | last post by:
I have a routine that updates a PictureBox image every x seconds. I do this by first loading an array with the path to all of the images. I then generate a random number to use as the index of the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.