Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am designing a windows form using c#. Here i have a button and i am trying to set a background image for it. The image is coming in designer page, but it is not appearing while running..
please help me..
thanks in advance.....
Posted
Updated 1-Dec-13 5:59am
v2
Comments
Dave Kreskowiak 1-Dec-13 11:58am    
Which is it?? You said both web page and winforms. They are mutually exclusive. So, which type of application are you writing?
vineeth raju 1-Dec-13 12:00pm    
sorry i mean windows forms.. i updated my question
vineeth raju 1-Dec-13 12:10pm    
actually i just want to give a background image for a button.
Ron Beyer 1-Dec-13 12:14pm    
How are you setting the background image?
vineeth raju 1-Dec-13 12:22pm    
yes i set into resources..

If you create a graphic file Resource like this:

1. context-click on your Project Name in the Solution Explorer treeview

2. select the Properties menu item at the bottom of the menu

3. click on the Resources item in the menu at left

4. open the Add Resource drop-down

5. select Add Existing File item

6. select your graphic file

Then, anytime you want to use the resource in code, you'll do something like:
button1.BackgroundImage = Properties.Resources.ResourceName;
Intellisense should show you the names of available resources as soon as you have typed "Properties.Resources."

If that's not working for you, then please respond with details on exactly how you have added the graphic file resource to your Project.
 
Share this answer
 
Well looking at your code why don't you replace you .BackgroundImage call
VB
this.button1.BackgroundImage = global::WindowsFormsApplication4.Properties.Resources.button1;
with

C#
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("MyProject.Resources.myimage.png"));

this.button1.BackgroundImage = bmp;
 
Share this answer
 
Comments
[no name] 1-Dec-13 13:45pm    
reason for the downvote?
Sergey Alexandrovich Kryukov 1-Dec-13 13:46pm    
This is a really, really, really bad way of using resources in .NET. What if you misspell the string? What if you moved/renamed the file? how would you remember where you should change it in all places you use it. The auto-generated resource source file (*.cs) is designed to put it all in order, it creates a static property of Image type (or, some other reasonable types, depending on the file).

OP just messed up something, why mislead him in using a bad way which won't give anything new but would develop a bad habit?

—SA
[no name] 1-Dec-13 13:50pm    
he could very easily make the string a global variable and when he changes it just change the variable string, then it is very easy to edit if you change the location of a resource.
Sergey Alexandrovich Kryukov 1-Dec-13 13:52pm    
Why saving an apparently bad solution by improving it just a bit? :-)
The whole idea make no sense. The good mechanism based on auto-generated code already exists and work well.
—SA

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