How do I change a image on a Button using Windows Forms?
You will most likely have to change the Button.Image
property in the code-behind. See the MSDN Documentation for information and a sample on how to do this.
how to change imagebox with a button | c# windows forms
Add an array of all posible images. Add a variable to store currently loaded image index in this array. And on buttonClick event, change the currently loaded image index, and load the image to picturebox. With this idea, you may have more than 2 images. Something like this:
public partial class Form1 : Form
{
string[] images = new string[] { "image1.bmp", "image2.bmp", "image3.bmp" };
int currentImageIndex = 0;
...
private void changebutton_Click(object sender, EventArgs e)
{
currentImageIndex++;
if (currentImageIndex == images.Length)
currentImageIndex = 0;
pictureBox.Image = Image.FromFile(images[currentImageIndex]));
}
...
}
pictureBox image change by clicking a button in win form app
You can call this method each click :
// it's global var refers to image
int count = 1;
// use count to load the next image
void loadNextPic()
{
string picPath = "../Pics/imageNum" + count.ToString() + ".jpg";
pictureBox.Image = Image.FromFile(picPath);
count++;
}
Remember you need to name the images as "imageNum1.jpg", "imageNum2.jpg" and so on.
How do I change one image to another upon clicking on a button
You can use a PictureBox and change the image property with your proper image and change a boolean property or variable each time you click on that.
private void pictureBox1_Click(object sender, EventArgs e)
{
if (flag)
pictureBox1.Image = WindowsFormsApplication21.Properties.Resources.GRAYSCALEsmalltub;
else
pictureBox1.Image = WindowsFormsApplication21.Properties.Resources.smalltub;
flag=!flag;
}
I hope this would solve your problem. Good Luck.
Change button image size in winforms app
You need to resize the Image
and then set it as the Buttons
Image:
// where 'MyImage' is the Image to display on the Button
this.Button1.Image = (Image)(new Bitmap(MyImage, new Size(32,32)));
Change button image after clicking it
Jason is right, you should use some kind of temporary storage to save your current bitmap.
The Tag property is useful in this kind of situations. Here a sample code: (Without error handling)
somewhere in your load event
PBr1.Tag = "cb.png";`
PBr1_1.Image = new Bitmap(Path.Combine("Logos\\Images", PBr1.Tag.ToString());
and then in button click
private void PBr1_1_Click(object sender, EventArgs e)
{
string imgPath = "Logos\\Images";
PBr1_1.Image.Dispose();
PBr1_1.Tag = (PBr1_1.Tag == "cb.png" ? "cg.png") : "cb.png") ;
Bitmap bm = new Bitmap(Path.Combine(imgPath, PBr1.Tag.ToString());
PBr1_1.Image = bm;
}
Related Topics
How to Hide a Property in Webapi
Get All Children to One List - Recursive C#
C# Multipart/Form-Data Submit Programmatically
Linq - Select Date from Datetime
How to Get Total Amount of Ram the Computer Has
Web API Returning Nested Json Values
Where Are the Properties.Settings.Default Stored
Thread.Sleep() Without Freezing the Ui
C# Find Highest Array Value and Index
Fast Way of Finding Most and Least Significant Bit Set in a 64-Bit Integer
Httpcontext.Current.Request.Files Is Always Empty
C# SQL Server Stored Procedure Parameter Return List
How to Remove Time Portion of Date in C# in Datetime Object Only
Return Json, But It Includes Backward Slashes "\", Which I Don't Want
What Could Cause an Assignment to Not Work
.Net Core - How to Properly Kill Started Process
Linq to SQL Left Outer Join Using Lambda Syntax and Joining on 2 Columns (Composite Join Key)