Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

How to change windows form icon from database using c# .net windows application?

How can we change icon dynamically or runtime using c# .net windows application?

Please help me.

Ankit Agarwal
Software Engineer
Posted
Comments
AndrewCharlz 31-Jul-14 5:51am    
its so simple just get the image from the db and convert the image into ico and then load the icon as this.icon = //from the image;
[no name] 31-Jul-14 8:40am    
I am trying this code for this.

string image = textBox1.Text;
Bitmap bmp = new Bitmap(image);
var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero);
thumb.MakeTransparent();
var icon = Icon.FromHandle(thumb.GetHicon());

SqlConnection con = new SqlConnection(cnString1);
con.Open();
SqlCommand cmd = new SqlCommand("ProcInsertLogo",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Logo", SqlDbType.Image).Value = icon;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Your Logo Successfully Uplodaed");
Form1 frmIcon = new Form1();
frmIcon.Icon = icon;

but its getting error:-
No mapping exists from object type System.Drawing.Icon to a known managed provider native type.

Please help how to solve it?

1 solution

See Bitmap.GetHicon Method[^].

1. Save your icon image in the DB as a .jpg image.
2. Retrieve your icon image into a Bitmap variable, say bitmap.
C#
Bitmap  bitmap;

3. Convert the Bitmap into an Icon handle.
C#
IntPtr  icon_handle = bitmap.GetHicon ( );

4. Create new Icon from the Icon handle.
C#
Icon    icon = Icon.FromHandle ( icon_handle );

5. Set Form Icon to the new icon.
C#
this.Icon = icon;

6. Destroy the icon, since the form creates its own copy of the icon.
C#
[ System.Runtime.InteropServices.DllImport ( "user32.dll",
                                             CharSet = CharSet.Auto ) ]
extern static bool DestroyIcon(IntPtr handle);

DestroyIcon ( icon.Handle );
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900