473,769 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.PrintD ocument 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.InvalidO perationExcepti on: The object is currently in use elsewhere.
at System.Drawing. Graphics.Dispos e(Boolean disposing)
at System.Drawing. Graphics.Dispos e()
at System.Drawing. Printing.PrintC ontroller.Print Loop(PrintDocum ent
document)
at System.Drawing. Printing.PrintC ontroller.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.PrintP ageEventArgs) Handles _mDoc.PrintPage
Dim shrinkPercentag e As Single
Dim hDC As IntPtr = e.Graphics.GetH dc()
e.Graphics.Rele aseHdc(hDC)
shrinkPercentag e = getShrinkPercen tage(hDC)
e.Graphics.Scal eTransform(shri nkPercentage, shrinkPercentag e)
e.Graphics.Draw Image(_images(_ printPageIterat or), New PointF(0, 0))
e.HasMorePages = (_printPageIter ator < (_images.Length - 1))
_printPageItera tor = _printPageItera tor + 1
End Sub

Private Function getShrinkPercen tage(ByVal hDC As IntPtr) As Single
Dim offx As Single = Convert.ToSingl e(GetDeviceCaps (hDC, PHYSICALOFFSETX ))
Dim offy As Single = Convert.ToSingl e(GetDeviceCaps (hDC, PHYSICALOFFSETY ))
Dim resx As Single = Convert.ToSingl e(GetDeviceCaps (hDC, HORZRES))
Dim resy As Single = Convert.ToSingl e(GetDeviceCaps (hDC, VERTRES))
Dim hsz As Single = Convert.ToSingl e(GetDeviceCaps (hDC, HORZSIZE)) / 25.4F
Dim vsz As Single = Convert.ToSingl e(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(_print PageIterator).H eight /
_images(_printP ageIterator).Ve rticalResolutio n) / 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.GetH dc()
e.Graphics.Rele aseHdc(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 getShrinkPercen tage 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 2407
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.PrintD ocument 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.InvalidO perationExcepti on: The object is currently in use elsewhere.
at System.Drawing. Graphics.Dispos e(Boolean disposing)
at System.Drawing. Graphics.Dispos e()
at System.Drawing. Printing.PrintC ontroller.Print Loop(PrintDocum ent
document)
at System.Drawing. Printing.PrintC ontroller.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.PrintP ageEventArgs) Handles _mDoc.PrintPage
Dim shrinkPercentag e As Single
Dim hDC As IntPtr = e.Graphics.GetH dc()
e.Graphics.Rele aseHdc(hDC)
shrinkPercentag e = getShrinkPercen tage(hDC)
e.Graphics.Scal eTransform(shri nkPercentage, shrinkPercentag e)
e.Graphics.Draw Image(_images(_ printPageIterat or), New PointF(0, 0))
e.HasMorePages = (_printPageIter ator < (_images.Length - 1))
_printPageItera tor = _printPageItera tor + 1
End Sub

Private Function getShrinkPercen tage(ByVal hDC As IntPtr) As Single
Dim offx As Single = Convert.ToSingl e(GetDeviceCaps (hDC, PHYSICALOFFSETX ))
Dim offy As Single = Convert.ToSingl e(GetDeviceCaps (hDC, PHYSICALOFFSETY ))
Dim resx As Single = Convert.ToSingl e(GetDeviceCaps (hDC, HORZRES))
Dim resy As Single = Convert.ToSingl e(GetDeviceCaps (hDC, VERTRES))
Dim hsz As Single = Convert.ToSingl e(GetDeviceCaps (hDC, HORZSIZE)) / 25.4F
Dim vsz As Single = Convert.ToSingl e(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(_print PageIterator).H eight /
_images(_printP ageIterator).Ve rticalResolutio n) / 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.GetH dc()
e.Graphics.Rele aseHdc(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 getShrinkPercen tage 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
1901
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 this error. Anyone have any clues?
4
5021
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, this problem requires some understanding of threading concepts. Basically, the class contained both a PictureBox object as well as a corresponding Image object (they both had a copy of the same picture). The following was used to access the...
0
2181
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 columns of the listview to my liking. When a row of the listview is selected, some controls are displayed on that row of the listview (so sort of like a property list thingy). In order to get the controls (specifically buttons) to display how I...
1
3234
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 get the best performance for the utility class, its members, as well as the System.Drawing.Graphics object, are static:
4
5709
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: e.Graphics.DrawString(MenuText.Substring(0, 1), underLinedFont, FontBrush, textLayoutRectangle, sf)
3
2206
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 deal with images upload. When the user is selecting an image, I save it as such together with a thumbnail version.
1
2321
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. When I try to se value of a Cell I do this: dgVuMain.Value = new Bitmap(global::FireFightersClient.Properties.Resources.water); Where water is a PNG format image. I sometimes get this error:
4
2292
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: "Object is currently in use elsewhere". If I comment the line where a I use the Activator.CreateInstance it doesn't happen. I tried to disposed the object created but nothing but If I do a simple new of my object without using reflection it...
0
2329
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 GUI(which must be another thread as it is the code-behind of a control. The application works but on some machines I get the following error at random time
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9981
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9850
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.