The object is currently in use elsewhere error when trying to prin

G

Guest

First off, I'm sorry if this isn't the write thread to post this message but
I couldn't find one dedicated to System.Drawing.

I'm developing an intranet application in VB.NET that uses a Hosted Windows
Form (HWF) dedicated to image manipulation. This HWF is also supposed to
allow the user to print an image to their default printer. I'm using a
System.Drawing.Printing.PrintDocument object to initiate printing, and
everything works locally on my development machine (Windows Server 2003).
When I deployed the code to my test environment, the other developers (also
on W2k3 boxes) were able to print fine from their machines. Our tester (on a
WinXP box) and our build server (on a Win2k3 box) were receiving the
following error:

System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Graphics.Dispose(Boolean disposing)
at System.Drawing.Graphics.Dispose()
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument
document)
at System.Drawing.Printing.PrintController.Print(PrintDocument
document)
..
..
..
etc...

The images that I'm passing to this print function are definitly not in use
anywhere else within the application. I've gone so far as to write the images
out to their own memory stream and then read them back in as new images
before passing them off to be printed.

Here's a little snippet of code that actually takes care of the printing:

Private Sub _mDoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles _mDoc.PrintPage
Dim shrinkPercentage As Single
Dim hDC As IntPtr = e.Graphics.GetHdc()
e.Graphics.ReleaseHdc(hDC)
shrinkPercentage = getShrinkPercentage(hDC)
e.Graphics.ScaleTransform(shrinkPercentage, shrinkPercentage)
e.Graphics.DrawImage(_images(_printPageIterator), New PointF(0, 0))
e.HasMorePages = (_printPageIterator < (_images.Length - 1))
_printPageIterator = _printPageIterator + 1
End Sub

Private Function getShrinkPercentage(ByVal hDC As IntPtr) As Single
Dim offx As Single = Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETX))
Dim offy As Single = Convert.ToSingle(GetDeviceCaps(hDC, PHYSICALOFFSETY))
Dim resx As Single = Convert.ToSingle(GetDeviceCaps(hDC, HORZRES))
Dim resy As Single = Convert.ToSingle(GetDeviceCaps(hDC, VERTRES))
Dim hsz As Single = Convert.ToSingle(GetDeviceCaps(hDC, HORZSIZE)) / 25.4F
Dim vsz As Single = Convert.ToSingle(GetDeviceCaps(hDC, VERTSIZE)) /
25.4F
Dim ppix As Single = resx / hsz
Dim ppiy As Single = resy / vsz
Dim bottom As Single = (vsz * 100.0F)
Dim top As Single = (offy / ppiy) * 100.0F
Return (bottom - top) / (_images(_printPageIterator).Height /
_images(_printPageIterator).VerticalResolution) / 100
End Function

Public Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hDc As IntPtr,
ByVal funct As Int32) As Int32

I've narrowed it down to the following two lines of code:

Dim hDC As IntPtr = e.Graphics.GetHdc()
e.Graphics.ReleaseHdc(hDC)

If I remove these two lines and then hard code the shrink percentage to .95,
it works. If I keep these two lines in, but hard code the shrink percentage
to .95 (instead of calling the getShrinkPercentage function), it breaks. My
hunch is that the handle isn't getting released, even though I'm calling
ReleaseHdc... but what is the most baffling to me is why it works on some
machines, but fails on others.

Any help would be most appreciated.

Thanks!

- Keith
 
G

Guest

I've still not been able to resolve this issue. I would appreciate any help!
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top