i have picturebox with picture cb.
PBr1_1.Image = new Bitmap(#"Logos\\Images\\cb.png");
I'd like to change image to cg.png and do some action when i click this image. I was trying something like that but without success:
private void PBr1_1_Click(object sender, EventArgs e)
{
if (PBr1_1.Image.ToString() == "cb.png")
{
PBr1_1.Image = new Bitmap(#"Logos\\Images\\cg.png");
// Do some stuff.
}
}
And then do the same when i click image with cb. To visualise this cb is black circle button image, and cg is green one.
How can i do this?
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;
}
Are you sure that "PBr1_1.Image.ToString()" really returns only the image name?
Maybe you should check this by writing PBr1_1.Image.ToString() to console or something like that
Related
How can I freeze the video displayed in the CameraControl when TakeSnapshot() has been called in order to display the fetched image?
Basically, I would like to rebuild the same capture behaviour as in the devexpress TakePictureDialog class in my own form, because in the TakePictureDialog it does not seem to be possible to store the user-selected camera device, which I need to do in my app, though.
I followed the instructions and examples in these articles:
https://www.devexpress.com/Support/Center/Question/Details/T269133/obtaining-the-last-selected-camera-device-and-restoring-it-in-a-takepicturedialog
https://documentation.devexpress.com/WindowsForms/114582/Controls-and-Libraries/Editors-and-Simple-Controls/Camera-Control
UserAppDataReigistry not persisting
Welcome to the stackoverflow.
CameraControl doesn't provide that functionality itself.
Either way you can use the Paint event to render the image, obtained via TakeSnapshot.
Something like this:
private void CameraControl1_Paint(object sender, PaintEventArgs e)
{
CameraControl c = sender as CameraControl;
if (isStopped && img != null)
e.Graphics.DrawImage(img, new Rectangle(0,0, c.Width, c.Height));
}
bool isStopped = false;
Bitmap img;
private void button1_Click(object sender, EventArgs e)
{
img = cameraControl1.TakeSnapshot();
cameraControl1.Stop();
isStopped = true;
}
Credits from devexpress.com
I get a pictureboxe and try to add it an action to change background Image upon click event happen, but there is no action. So, this is the code:
pieces bishopBB = new pieces();
public Form1()
{
// object of picturebox
bishopBB.Location = new Point(300, 455);
bishopBB.Parent = this;
bishopBB.Click += new System.EventHandler(pictureboxes_Click)
InitializeComponent();
}
private void pictureboxes_Click(object sender, EventArges e)
{
backgroundImage = Properties.Resources.black;
}
Looking at the name and other indicators I would assume (and hope) that pictureboxes_Click is a common click handler for many PictureBoxes.
So to access the one that has been clicked you need to cast sender to PictureBox and then set the BackgroundImage:
private void pictureboxes_Click(object sender, EventArges e)
{
((PictureBox)sender).BackgroundImage = Properties.Resources.black;
}
I'm a little amazed though that your spelling of backgroundImage compiles. The correct spelling BackgroundImage refers to the current class, usually the Form and it should also show, unless your Form has a black background already..
To change the current image of the PictureBox control, you need to refer to the control and reference the BackgroundImage property (not 'backgroundImage'):
private void pictureboxes_Click(object sender, EventArges e)
{
this.pictureboxes.BackgroundImage = Properties.Resources.black;
}
To change the background image of the form:
private void pictureboxes_Click(object sender, EventArgs e)
{
this.BackgroundImage = Properties.Resources.black;
}
To do this you can use the on_click property of the picturebox. You can then use the like picbox.image = whatever the image location is
I am trying to do the following action with pictureboxes:
Search for a picturebox that has a special image, my method for this returns a number of the box that has this image.
Example:
if (picturebox1.Tag == "special") {
return 1; //returns value 1 as in picturebox1 contains it.
}
Now when I click on pictureBox2 I want it to switch with the picturebox that contains the special picture (I have a total of 10 pictureboxes with pictures, only one box contains a special one)
Click event on picturebox2:
private void pictureBox2_Click(object sender, EventArgs e)
{
performSwitch(2); //2 meaning picturebox "2"
}
Method it calls:
public void performSwitch(int pictureBoxWaarde)
{
switch (pictureBoxWaarde)
{
case 2:
pictureBox10.Image = pictureBox2.Image; //else picture overwrites
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = pictureBox10.Image;
break;
}
}
This code works however I dont want to make TONS of if-else statements for every combination there is.. So to make it 'smart' I will need something like this:
public void performSwitch(int pictureBoxWaarde)
{
switch (pictureBoxWaarde)
{
pictureBox10.Image = pictureBox2.Image; //else picture overwrites
pictureBox2.Image = pictureBox"+findBlackBox+".Image;
pictureBox"+findBlackBox()+".Image = pictureBox10.Image;
break;
}
}
If this would work it would get the special box number and work without the need of lots of if-else. How would I do this?
I'm not sure whether I fully understand what you're trying to achieve, but you can always find a control by its name.
Let's say that you have a variable string m_specialPictureBoxName; that always contains the name of the current special picture box, you could do the following:
public void performSwitch(int pictureBoxWaarde)
{
// Get current special picture box
PictureBox currentSpecial = this.Controls.Find(m_specialPictureBoxName, true) as PictureBox;
// Get new special picture box
m_currentPictureBoxName = String.Format("pictureBox{0}", pictureBoxWaarde);
PictureBox clicked = this.Control.Find(m_currentPictureBoxName, true) as PictureBox;
// Swap images
Image temp = currentSpecial.Image;
currentSpecial.Image = clicked.Image;
clicked.Image = temp;
}
Also I'm swapping the image without using the temporary pictureBox10 assignment - I'm not sure whether you want pictureBox10 to get the image. If you do, you can replace it with your previous code instead of the Image temp = ...line.
Assign all the PictureBox event handlers to a single method.
Write the method like this
private void ApictureBox_Click(object sender, EventArgs e)
{
var clickedPicture = sender as PictureBox;
// iterate through all picture controls of the current form
for (var pic in this.Controls.OfType<PictureBox>())
if (pic.Tag=="special")
{
var temp = clickedPicture.Image;
clickedPicture.Image = pic.Image;
pic.Image = temp;
}
}
For the first step, you can either do it manually, or use a similar loop to assign the event handlers in the form constructor.
Here is my question:
Im making a booking cinema system in c#, windows form
Let's say i have 5 columns of 5 rows of pictureboxes that on form load get their value, avaliable or not from the database.
The user then click on the seat he wants (and the image of the pictuebox change) and press a submit button.
How i can check the image of every picturebox (to determine if he want this seat or not) together?
I can do something like this
if (picturebox11.image=="seatchecked"){seats[]+=11;}
if (picturebox12...
But im wondering if there is another faster way to do it. (the position of the pictureboxes is fixed if that helps)
I have done this so far:
private void button1_Click(object sender, EventArgs e)
{
List<PictureBox> pb = new List<PictureBox>();
pb.Add(seat11);
pb.Add(seat12);
pb.Add(seat13);
pb.Add(seat14);
pb.Add(seat15);
pb.Add(seat21);
pb.Add(seat22);
pb.Add(seat23);
pb.Add(seat24);
pb.Add(seat25);
pb.Add(seat31);
pb.Add(seat32);
pb.Add(seat33);
pb.Add(seat34);
pb.Add(seat35);
for (int i = 0; i < 20; i++) {
pb[i].Click += pictureBox_Click;
}
}
void pictureBox_Click(object sender, EventArgs e)
{
this.pictureBox.Image = ArgyroCinema.Properties.Resources.seatred;
}
Store each PictureBox in a list and iterate through them. Also, when the user selects/deselects a seat, change the Tag property of the PictureBox as, at the moment, you're trying to compare a string to an Image (picturebox11.Image returns an Image object).
List<PictureBox> pb = new List<PictureBox>();
pb.Add(pictureBox1);
pb.Add(pictureBox2);
//etc..
Alternatively, you can use the methods suggested here to get all PictureBox objects in your form to save you having to type it out above.
Then just iterate through them and read their Tag property. In this case I've used true to represent that they want the seat but Tag is an object type, so you can use whatever type you like.
foreach(PictureBox p in allPictureBoxes)
{
if((bool)p.Tag == true)
{
//seat wanted
}
else
{
//seat not wanted
}
}
Update from comments
void pictureBox_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
if(pb != null)
pb.Image = ArgyroCinema.Properties.Resources.seatred;
}
I'm trying to add some background images to a few buttons in my Win Forms application. The three images are different sizes (ie pixel dimensions don't match, one is 128x128 and another is 256x256). I need the buttons to be identical in size (otherwise the GUI is horribly asymmetrical). Without changing the actual image files, how can I get the images to scale with button size?
I've tried creating my own class, and adding an event handler for the button resize event, but that doesn't seem to work. My code:
class CustomButton : Button {
internal void CustomButton_Resize( object sender, EventArgs e ) {
if ( this.BackgroundImage == null ) {
return;
}
var pic = new Bitmap( this.BackgroundImage, this.Width, this.Height );
this.BackgroundImage = pic;
}
}
and in the form:
this.buttonOne.Resize += new System.EventHandler(this.buttonOne.CustomButton_Resize);
Forgot to mention, the above code does not resize the images at all. The buttons still need to have different sizes to display the images completely.
Easiest way to add a background image to a .NET Button object and scale it to fit
I used this method to avoid any additional coding of new classes and event handlers. This helped me also avoid converting all Button objects into Image objects.
Add image to your Resources.resx file.
Click on your chosen button.
Navigate to the BackgroundImage property and choose the image you imported into the project's resources.resx file.
Navigate to the BackgroundImageLayout property and choose Stretch.
Make sure you don't have anything entered for the Image and Text properties or else they will interfere with your new background image.
The easy programmatic way
Say I have a button btn1, Following code is working perfectly in visual-studio-2010.
private void btn1_Click(object sender, EventArgs e)
{
btn1.Width = 120;
btn1.Height = 100;
}
void btn1_Resize(object sender, EventArgs e)
{
if ( this.BackgroundImage == null )
return;
var bm = new Bitmap(btn1.BackgroundImage, new Size(btn1.Width, btn1.Height));
btn1.BackgroundImage = bm;
}
The better way
You can add eventHandler in the constructor of your custombutton (just to ensure that you are adding eventhandler correctly)
class CustomButton : Button
{
CustomButton()
{
this.Resize += new System.EventHandler(buttonOne.CustomButton_Resize);
}
void CustomButton_Resize( object sender, EventArgs e )
{
if ( this.BackgroundImage == null )
return;
var pic = new Bitmap( this.BackgroundImage, new Size(this.Width, this.Height) );
this.BackgroundImage = pic;
}
}
Now when you will resize the button anywhere your image will get fit(scaled) to its new size.
You could start with something like this...
public class ImageButton : Control
{
public Image backgroundImage;
public Image BackgroundImage
{
get
{
return backgroundImage;
}
set
{
backgroundImage = value;
Refresh();
}
}
public ImageButton()
{
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
if(BackgroundImage != null)
e.Graphics.DrawImage(BackgroundImage, 0, 0, Width, Height);
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//base.OnPaintBackground(pevent);
}
}
You can just handle paint and draw the image yourself. You may also try using a PictureBox or some other control which has more scaling options