Strange exception in GDI +

  • Thread starter =?ISO-8859-2?Q?=A3ukasz?=
  • Start date
?

=?ISO-8859-2?Q?=A3ukasz?=

Hi,

I'm writing application which is drawing an icon. Then it's displayed in
tray by NotifyIcon. And there is a problem, because sometimes an
exception is thrown:

Type: System.Runtime.InteropServices.ExternalException

Message:
A generic error occurred in GDI+.
Source:
System.Drawing
StackTrace:
at System.Drawing.Bitmap.GetHicon()
TargetSite:
IntPtr GetHicon()

Does enybody knows what's going on with this exception???

Code:

Bitmap bmp = new Bitmap(16, 16);
Graphics gr = Graphics.FromImage(bmp);
gr.Clear(Color.Transparent);
gr.FillRectangle(Brushes.White, 1, 2, 13, 12);
gr.DrawRectangle(Pens.Gray, 0, 1, 14, 13);
gr.DrawString(number.ToString("00"), this.Font, Brushes.Black, .0f, 1.0f);
gr.Dispose();
notifyIcon.Icon = (Icon)Icon.FromHandle(bmp.GetHicon()).Clone();
notifyIcon.Visible = true;

£ukasz
 
J

Johann Blake

£ukasz

Works fine on my computer. Make sure your variable, number, is set to
some correct value. Have you tried testing this snippet of code in an
empty project without all of the other code you have?

Best Regards
Johann Blake
 
?

=?ISO-8859-2?Q?=A3ukasz?=

Johann Blake
Works fine on my computer. Make sure your variable, number, is set to some correct value.
Number is always correct, I've checked that.
Have you tried testing this snippet of code in an
empty project without all of the other code you have?
Yes, and it's always working there.
Only in my application it doesn't work (but only sometimes).
And I still don't know why. :-(
Are there any other ways to draw an icon?

-----

Maybe more code would be useful:

private void buttonStart_Click(object sender, System.EventArgs e)
{
stop = false; // indicating when to stop the thread
buttonStart.Enabled = false;
threadStart = new Thread(new ThreadStart(StartThread));
threadStart.Start();
buttonStop.Enabled = true;
}

private void StartThread()
{
try
{

... some code ...

lock (this)
{
notifyIcon.Icon = null;
notifyIcon.Text = "0";
notifyIcon.Visible = true;
}
for (int number = 0; number < 100; number++)
{
lock (this) if (stop) return;

... some code ...

lock (this)
{
notifyIcon.Text = number.ToString();
Bitmap bmp = new Bitmap(16, 16);
Graphics gr = Graphics.FromImage(bmp);
gr.Clear(Color.Transparent);
gr.FillRectangle(Brushes.White, 1, 2, 13, 12);
gr.DrawRectangle(Pens.Gray, 0, 1, 14, 13);
gr.DrawString(number.ToString("00"), this.Font, Brushes.Black,
..0f, 1.0f);
gr.Dispose();
notifyIcon.Icon = (Icon)Icon.FromHandle(bmp.GetHicon());
notifyIcon.Visible = true;
bmp.Dispose();
}

... some code ...

}
}
catch (Exception e)
{
Exception ex = e;
while (e != null)
{
string str = "Exception:\n" + e.GetType().FullName;
str += "\n\nMessage:\n" + e.Message;
str += "\n\nSource:\n" + e.Source;
str += "\n\nStackTrace:\n" + e.StackTrace;
str += "\n\nTargetSite:\n" + e.TargetSite;
MessageBox.Show(str, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
e = e.InnerException;
}
}
finally
{
lock (this) notifyIcon.Visible = false;
}
}

Best Regargs
£ukasz
 
Top