473,396 Members | 2,013 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,396 software developers and data experts.

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

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(Prin tDocument
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
Dec 13 '05 #1
1 2388
I've still not been able to resolve this issue. I would appreciate any help!
Thanks.

"kCura" wrote:
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(Prin tDocument
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

Jan 2 '06 #2

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

Similar topics

4
by: KevinBradly | last post by:
Building and running a VB or C# graphics application within VisStudio is fine. Running the .exe outside VisStudio on the same machine is fine. But trying to run the .exe on another machine produces...
4
by: 6tc1 | last post by:
Hi all, I have just finished debugging a windows application and have solved the problem - however, I want to be sure that I understand the problem before I move on. Before I detail the problem,...
0
by: Sam Barham | last post by:
I have a ListView control, for which I have overwritten the WndProc method to gain access to the WM_PAINT message and generate my own OnPaint and OnPaintBackground messages, in order to colour the...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
4
by: JJ | last post by:
When I run my form prog, if I maximise, then minimise (i..i. force the GUI to redraw) I get the following error: "The object is currently in use elsewhere" At this line: ...
3
by: boeledi | last post by:
Dear All, (First of all this is not a c# piece of code but it does not really matter). I would really appreciate if someone could help me. I am developing an ASP.NET web site and I have to...
1
by: sheperson | last post by:
Hi, I have an 8*8 DataGridView (8 rows and 8 columns). All of the columns are of type DataGridViewImageCell. I want to simulate a FireFighters game and for showing the map I use DataGridView....
4
by: =?Utf-8?B?Qm9uaQ==?= | last post by:
Hi, I got this problem. I'm implementing a pluggable winform program. My plugins are usercontrol and I load them in my program through a interface. Now if I close my application an error occurs:...
0
by: para15000 | last post by:
Hello I have a C# desktop application in which one thread that I create continously gets an image from a source(it's a digital camera actually) and puts it on a panel(panel.Image = img) in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.