Click here to Skip to main content
16,017,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I'm working on a simple project. I need to add images(jpg or bmp) to buttons.

My question:

I have some images in my local drive. I know that I need to add these images to current project that I'm working before it can be used.

Since I'm also having > 2 buttons, I guess it would be better to add the images to some kind of container and use its index to assign it to the appropriate button.

1) How can I add the images to the project?
2) How to add these images to buttons?

Thanks,
Skunkhead :)
Posted

1 solution

How to add images to the project?

My main recommendation: do not do it using Visual Studion image editor. The images will be embedded in the resource file; it will render the resource file unmanageable and hard to support. Also, there are some much better free editors around. (I would recommend GIMP and InkSkape (vector, but supports exports to anything, importantly to XAML)). So, you create some files and put them in some resource directory within your project. In the same directory, create new *.resx resource and click "Add Resource" -> "Add Existing File", proceed. Your graphic files will be added to the project and to the resource. Use auto-generated C# files under resource node to access the images. Prefer PNG.

How to add images to buttons?

Add a button. Click its Properties. Locate the property Image and click on the "ellipsis" button. It will show you your resources and selection of the image to associate with the button.

Programmatically...

C#
Panel parent = //... for example, panel is a button's parent
Button button = new Button();
button.Image = //... image reference, see resource's auto-generated class
button.Text = "Some &Text";
button.Width = //...
button.Top = //...
button.Left = //...
parent.Controls.Add(button); //at this moment it is shown


—SA
 
Share this answer
 
v3
Comments
skunkhead 8-Feb-11 0:44am    
Hi SA,
Thanks for your reply.
Well...
"So, you create some files and put them in some resource directory within your project"
What files i need to create(is it image files) and to which resources directory i need to it in?i dont have any resource folder now.
Sergey Alexandrovich Kryukov 8-Feb-11 0:46am    
Same as your project (I don't recommend it) or Solution Explorer -> You project node -> Context Menu -> Add... -> New Folder.
Sergey Alexandrovich Kryukov 8-Feb-11 0:49am    
You see, adding a file is two-fold, add it physically and add to the project. (Yes, files a graphic; I recommend PNG.) When a file is in some other project, adding it creates a copy. Do you need a copy? So, copy it manually where they will be in project. Directory structure is all yours, but recommended to be at the level of your *.*proj file and deeper.
--SA
Sergey Alexandrovich Kryukov 8-Feb-11 0:51am    
Would you do it quick and report back if you need some corrections. I'll be able to help during next 1/2 hours or so before I sleep...
--SA
skunkhead 8-Feb-11 0:52am    
ok, now I need to add '*.resx' file to the same folder, rite?How am I suppose to add?right click the new folder-> Add-> New Item->add test.resx?

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