J
Joshua Flanagan
I am building a "smart client" WinForms executable that will be launched
directly from a webpage. I do not want to require any client-side
installation, or .NET security policy configuration.
I am trying to dynamically generate the image for a NotifyIcon.
I could not find any way to get a Graphics object for an Icon, so I decided
to create a Bitmap. I can create the Bitmap and generate the image:
Bitmap iconImage = new
Bitmap(16,16,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(iconImage);
g.Clear(Color.Transparent);
// image generation is simplified for this example
g.FillEllipse(Brushes.Orange, 0,0,16,16);
g.Dispose();
but now I cannot figure out how to use the image in my NotifyIcon.
I tried:
trayIcon.Icon = Icon.FromHandle(iconImage.GetHicon());
but that throws a SecurityException when the code is run from a webpage.
Apparently the Icon.FromHandle() method is considered UnsafeCode.
So then I tried:
System.IO.MemoryStream iconStream = new System.IO.MemoryStream();
iconImage.Save(iconStream, System.Drawing.Imaging.ImageFormat.Icon);
trayIcon.Icon = new Icon(iconStream, 16, 16);
but the iconImage.Save() line throws the following exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: encoder
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
Do I need some special Icon encoder codec installed, or is this a bug in the
Framework? It looks like the ImageFormat overload is trying to call the
ImageCodecInfo overload, but it is passing a null value for the encoder.
Does anyone know how to make one of my approaches work?
Or is there a different approach that will allow me to dynamically generate
an image for my tray icon (within the security constraints of untrusted code
launched from a web page)?
Any help would be appreciated. Thanks.
Joshua Flanagan
directly from a webpage. I do not want to require any client-side
installation, or .NET security policy configuration.
I am trying to dynamically generate the image for a NotifyIcon.
I could not find any way to get a Graphics object for an Icon, so I decided
to create a Bitmap. I can create the Bitmap and generate the image:
Bitmap iconImage = new
Bitmap(16,16,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(iconImage);
g.Clear(Color.Transparent);
// image generation is simplified for this example
g.FillEllipse(Brushes.Orange, 0,0,16,16);
g.Dispose();
but now I cannot figure out how to use the image in my NotifyIcon.
I tried:
trayIcon.Icon = Icon.FromHandle(iconImage.GetHicon());
but that throws a SecurityException when the code is run from a webpage.
Apparently the Icon.FromHandle() method is considered UnsafeCode.
So then I tried:
System.IO.MemoryStream iconStream = new System.IO.MemoryStream();
iconImage.Save(iconStream, System.Drawing.Imaging.ImageFormat.Icon);
trayIcon.Icon = new Icon(iconStream, 16, 16);
but the iconImage.Save() line throws the following exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: encoder
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
Do I need some special Icon encoder codec installed, or is this a bug in the
Framework? It looks like the ImageFormat overload is trying to call the
ImageCodecInfo overload, but it is passing a null value for the encoder.
Does anyone know how to make one of my approaches work?
Or is there a different approach that will allow me to dynamically generate
an image for my tray icon (within the security constraints of untrusted code
launched from a web page)?
Any help would be appreciated. Thanks.
Joshua Flanagan