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

Home Posts Topics Members FAQ

Problem with System.Drawing. Graphics as a static object

Hi,

I'm getting "object is currently in use elsewhere" when I use
System.Drawing. Graphics.Mesure String.

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:

internal sealed class Utils
{
static System.Drawing. Bitmap oBmp;
static System.Drawing. Graphics oGrf;
.....
.....
}
The members are initialized in the static constructor:

static Utils()
{
oBmp = new System.Drawing. Bitmap(100, 150); //dummy numbers
oGrf = new System.Drawing. Graphics.FromIm age(oBmp);
oGrf.PageUnit = System.Drawing. GraphicsUnit.Pi xel;
.....
.....
}
In that way there is only one object for the controls.
Every control calls this method one time when the control is loading:

public static float CalcStringLengt h(string InText)
{
System.Drawing. SizeF oSizeF = oGrf.MesuereStr ing(InText, oFontInfo);
return oSizeF.Width;
}

The problem is that when 10 users try to activate 10 controls that call to
CalcStringLengt h I get this error:
"the object is currently in use elsewhere"
I know this object is meant to be disposed but because it is static I have
no idea when to dispose it...

This didn't help either:
public static float CalcStringLengt h(string InText)
{
try
{
System.Drawing. SizeF oSizeF = oGrf.MesuereStr ing(InText, oFontInfo);
return oSizeF.Width;
}
catch
{
// restart the graphic object
oGrf.Dispose;
oGrf = System.Drawing. Graphics.FromIm age(oBmp);
return CalcStringLengt h(InText);
}
}
Is there any way that I can work with the graphic object as static object
without getting this error?

Thanks in advance,
Hadar.

Nov 17 '05 #1
1 3234
You need to make it public rather than static, and require a separate
instance of it for each process/thread that needs it.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
The more I learn, the less I know.

"Hadar" <cr****@bezeqin t.net> wrote in message
news:OB******** ******@tk2msftn gp13.phx.gbl...
Hi,

I'm getting "object is currently in use elsewhere" when I use
System.Drawing. Graphics.Mesure String.

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:

internal sealed class Utils
{
static System.Drawing. Bitmap oBmp;
static System.Drawing. Graphics oGrf;
....
....
}
The members are initialized in the static constructor:

static Utils()
{
oBmp = new System.Drawing. Bitmap(100, 150); //dummy numbers
oGrf = new System.Drawing. Graphics.FromIm age(oBmp);
oGrf.PageUnit = System.Drawing. GraphicsUnit.Pi xel;
....
....
}
In that way there is only one object for the controls.
Every control calls this method one time when the control is loading:

public static float CalcStringLengt h(string InText)
{
System.Drawing. SizeF oSizeF = oGrf.MesuereStr ing(InText, oFontInfo);
return oSizeF.Width;
}

The problem is that when 10 users try to activate 10 controls that call to
CalcStringLengt h I get this error:
"the object is currently in use elsewhere"
I know this object is meant to be disposed but because it is static I have
no idea when to dispose it...

This didn't help either:
public static float CalcStringLengt h(string InText)
{
try
{
System.Drawing. SizeF oSizeF = oGrf.MesuereStr ing(InText, oFontInfo); return oSizeF.Width;
}
catch
{
// restart the graphic object
oGrf.Dispose;
oGrf = System.Drawing. Graphics.FromIm age(oBmp);
return CalcStringLengt h(InText);
}
}
Is there any way that I can work with the graphic object as static object
without getting this error?

Thanks in advance,
Hadar.

Nov 17 '05 #2

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

Similar topics

0
309
by: Franco Gustavo | last post by:
Hi, How can I execute 2 GDI steps without update the screen and update them togheter?. Basically I have a form and I want to change the Form Region and change the Form Location, later show the changes on the screen. On the Following code you can see that the code is executed but where the window was there is the rest of the window that wasn't cleaned, if you move
3
4851
by: Jeroen Ceuppens | last post by:
Hi, When i do this in my programma, in the contstructor of the form: g.DrawImage(i,new Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel); it doens't paint it ;( but if I make a button with the same, after the click it paints it, why the problem, i can't get out of it.........
11
6198
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint . The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored What should i do in the Onpaint to make sure the background (the form) is restored correctly ? This problem already keeps...
1
1477
by: Bardo | last post by:
Hi all Ok first of me new to C#sharp. At the moment I am still learning the language. The problem I am running into is that I just can't seem to get a picture embedded into my program and then read it. Everytime I get a error from the debugger. I tried all kinds of thinks but can figure out what is going wrong. Using the C#builder from Borland I get it working, but not with VS. What am I doing wrong ? I included the program so you can see...
9
2823
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am facing. Please please help me to solve this as soon as possible. So here we go ... I am not able to take the screen shot of the windows form based "Smart
1
7179
by: jty202 | last post by:
I know alot of people have the problem with indexed pixel format. I hope someone can show me the solution to this.I am have problem with graphics with the following code giving the error: "A Graphics object cannot be created from an image that has an indexed pixel format." 'My code
3
4029
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user selects? thanks...
2
2143
by: JR | last post by:
Hi, I need to change the notifyicon in a countdown. after about 3000-5000 times there is an error. Here is all the code. what is wrong please help
2
2858
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the fragment of code I think to be the cause of the problem. Then, I will post the code to all 3 of my java files in their entirety should you wish to reference them. Any help would be much appreciated. Also, if it would help fix my error, I would...
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
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
8862
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7396
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.