1

I want move an image in one picture box to another and vice versa on a button click, I'm pretty sure my code should work in theory, but it just won't execute for some reason. Any help would be much appreciated please!! Here's my code:

    int chicken_move = 0;

    private void button_Chicken_Click(object sender, EventArgs e)
    {
        chicken_move++;
        if (chicken_move > 1)
        {
            chicken_move = 0;
        }

        switch (chicken_move)
        {
            case 0:

                pictureBox_Micro.Image = pictureBox_Uncooked.Image;
                pictureBox_Uncooked.Image = null;

                break;

            case 1:

                pictureBox_Uncooked.Image = pictureBox_Micro.Image;
                pictureBox_Micro.Image = null;

                break;
        }
L. Mitchell
  • 61
  • 1
  • 10
  • Use variables first off, such as two static bitmaps. Use dispose on bitmaps, not just null, and it's winforms, so you need to invalidate, and maybe a doevents – Trey May 11 '17 at 19:22

3 Answers3

2

You need to use swap technique to change picture. Otherwise, you will lost it after first click.

 public partial class Form1 : Form
    {
        private bool isFirstOne;
        Image forSwap;
        public Form1()
        {
            InitializeComponent();

        string path1 = @"C:\Users\...\...\somePic.png";

        pictureBox1.Image = Image.FromFile(path1);

        forSwap = null;
        isFirstOne = false;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        switch (isFirstOne)
        {
            case true:
                forSwap = pictureBox2.Image;
                pictureBox1.Image = pictureBox2.Image;
                pictureBox2.Image = null;
                break;

            case false:
                forSwap = pictureBox1.Image;
                pictureBox2.Image = pictureBox1.Image;
                pictureBox1.Image = null;
                break;
        }
        isFirstOne = !isFirstOne;
    }
}
Amiraslan
  • 794
  • 1
  • 8
  • 19
  • In terms of the image file path, if I want to run this another computer will it still be able to extract the image, and if not, how do I set up the path in order that it will run on the other computer? – L. Mitchell May 11 '17 at 19:50
  • It won't find the image in another computer if you use it in this way. You can add image to Resources(in properties) and then, it will work in any computer without needing the path – Amiraslan May 11 '17 at 19:59
  • How can I get around this? – L. Mitchell May 11 '17 at 20:00
  • 1
    After adding Resources.resx, you can use it in this way: pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.aslan; [you can find how to upload image to Resources in here](http://stackoverflow.com/a/9316734/6514270) – Amiraslan May 11 '17 at 20:02
  • Awesome thank you!! Sorry last question, how would I go about zooming the image to fit the picture box in the coding environment? – L. Mitchell May 11 '17 at 20:08
  • 1
    `pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; pictureBox1.SizeMode = PictureBoxSizeMode.Normal; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;` You can use one of these ways to adjust image dimensions inside picture box – Amiraslan May 11 '17 at 20:14
1

Try not setting the Image of one pictureBox to the Image of the other.

Try setting the Image to a reference to the actual image (file, etc.).

Or try using pictureBox.ImageLocation to set it.

JUmlauf
  • 38
  • 1
  • 5
  • I think it's a big stretch calling this an answer, but... yeah. – Trey May 11 '17 at 19:32
  • 2
    It was my first one! How can I improve? [It was intimidating to submit it, tbh.] – JUmlauf May 11 '17 at 19:45
  • Well, usually the answer has code examples, or ... an answer. :-) Just kidding but seriously, an example with each of your suggestions would probably help anyone that has this issue in the future. – Trey May 11 '17 at 19:49
  • Got it. Thank you for your feedback. I appreciate it. – JUmlauf May 11 '17 at 19:51
  • My pleasure, your doing a lot better than I was at that point, I think my first post I cussed everyone out :-) – Trey May 11 '17 at 19:58
1

Considering you set the chicken_move variable as global, there is nothing wrong with the code. Try providing image file location:

pictureBox_Micro.ImageLocation = "UncookedImage.jpg";

pictureBox_Uncooked.ImageLocation = "MicroImage.jpg";
cona mx
  • 55
  • 6