Create an Icon from an Image safely

  • Thread starter Joshua Flanagan
  • Start date
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
 
Y

Ying-Shen Yu[MSFT]

Hi Joshua,

As Andy said, currently the Image.Save method does not support saving image
into Icon format, since the underlying GDI+ does not have an encoder for
ICO format.
You need to convert the image into Icon stream by your self. Here is some
information about the Icon file format, maybe it will be helpful to you:
<Icons in Win32>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/
msdn_icons.asp

Thanks!
Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
J

Joshua Flanagan

You need to convert the image into Icon stream by your self. Here is some
information about the Icon file format, maybe it will be helpful to you:
<Icons in Win32>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/
msdn_icons.asp

Ok, I took your advice and created code to do the conversion from a
bitmap stream to an icon stream. Thanks for the MSDN link, it was very
helpful.

If anyone needs it, the solution is posted on the Code Project at:
http://www.codeproject.com/useritems/safeicon.asp

Joshua Flanagan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top