I have an SQL2005 table loaded with ICON (image) BLOBs. I need to dynamically add ToolStripMenuItems to my Windows form including the same row Icon from the database table.

This is a Plugin system where I pull the plugin file (assembly) names from the database according to the operator's rights and place them under a Modules top menu item.

Creating the additional menu items was an easy task, but assigning the Menu image from the IMAGE type column has me frustrated.
The database was populated from ICON files through a Delphi application. So easy to do in Delphi, but I am at a loss in how to accomplish this in C# (VS2005).

Thanks for any Assistance,
Jerry

Answering myself, so others can see how I did this.

Create the new Menuitem, and populate it from the database fields.
Read the ICON blob field into a memory stream. And get its length. This was the killer, other examples on the net were using other ways to obtain the image length, which resulted in dropping the last couple bytes. Using the stream.Length took care of that.

Create and stream the data into an Icon object.
Then populate the new menuitem image property with the Icon ToBitMap method.

childMenu = new ToolStripMenuItem();
childMenu.Text = (string)rdr["MODULE_NAME"];
// get the image from the Database Column name is "ICON"
byte[] byteData = new byte[0];
byteData = (byte[])rdr["ICON"];
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteData, true);
long n = stream.Length; 
 
System.Drawing.Icon iicon = new System.Drawing.Icon(stream);
childMenu.Image = iicon.ToBitmap();
 
parentMenu.DropDownItems.Add(childMenu);
stream.Close();

hi
i have some situation but i need to populate my menu items from xml

for text its working fine, gut how can i add icons?

foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
               
                ToolStripMenuItem childMenu = new ToolStripMenuItem();
                toolStripDropDownButton1.DropDownItems.Add(childMenu);
                childMenu.Text = (node.Attributes["TotoID"].Value);
            }

Danke

Been a long time since I was on this board... Just viewed your question by accident.
Ok, one thing to always remember when dealing with images, or icons is they are always stored as an array of byte. Therefore, you would store the byte array in the XML file, and pull it out as a byte array, then load it into your menu icon property.

Let me know if this helps. If you get stuck, maybe I can whip up a short example.

// Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.