简体   繁体   中英

I cannot change image in picturebox c#

I have made a yatzy game in a console application, and I am currently trying to make one in a forms application.
This is what I have so far:

namespace Yatzy
{
   public partial class Form1 : Form
   {
       public static Random kast = new Random();

       public static int kast1 = kast.Next(1, 7);
       public static int kast2 = kast.Next(1, 7);
       public static int kast3 = kast.Next(1, 7);
       public static int kast4 = kast.Next(1, 7);
       public static int kast5 = kast.Next(1, 7);
       public static int kast6 = kast.Next(1, 7);


       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {

       }


       private void pictureBox1_Click(object sender, EventArgs e)
       {
           if (kast1 == 1)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning1.png"); 
           }
           else if (kast1 == 2)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning2.png");
           }
           else if (kast1 == 3)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning3.png");
           }
           else if (kast1 == 4)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning4.png");
           }
           else if (kast1 == 5)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning5.png");
           }
           else if (kast1 == 6)
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning6.png");
           }
           else
           {
               this.pictureBox_terning1.Image = new Bitmap(@"\Pics\terning0.png");
           }
       }


   }
}

As you can see, I define the "kast1" and such in the form, and depending on the outcome, it should display different images in the picturebox. I have looked at every post I could find, and all of the solutions were fuss.

I have tried without "this." and I've tried with "= image.FromFile("Pics\\terning#.png");"
Nothing works.

更改图片源后,您可能需要在图片框控件上调用Refresh()。

There is no need to use Bitmap object just to load images in the PictureBox.Perhaps the more cleaner way of loading images is by using the Load Method of PictureBox .

If I assume the images are in same directory as your application,enclosed in another folder,this would have done the job easily;

this.pictureBox_terning1.Load(@"\Pics\terning1.png"); 

The Load method requires a valid path that points to an image as argument,and with this there is no need to call Refresh after loading each image.It doesn't matter if the path is absolute or relative,but the thing that matters is that the path should point to an image. But I would suggest you to create an absolute path to your executable like this;

string apppath=Application.StartupPath;
string imagepath=@"\Pics\terning1.png";
this.pictureBox_terning1.Load(apppath+imagepath); 

As you mentioned that even if no errors are encountered,the images are not being loaded,for such a case debugging would be an ideal technique to find where the program runs out of order.

It is most likely that the images you want to load are not at the path you are looking at.

 image.FromFile("Pics\terning#.png");

Expects that your images reside in {youprojectfolder}\\bin\\DebugORRelease\\Pics.

new Bitmap(@"\Pics\terning1.png");

Expects that you images reside in {most likely c:}\\Pics.

So i would suggest to check this first and if this does not work add a Breakpoint (F9) and start debugging (F5) see MSDN for an introduction in debugging

I would also suggest you to replace your if, else if construct with a switch .

Try this code, which I have tried and works(it will get the bin directory of your app then you can replace folders with image directory):

  private void pictureBox1_Click(object sender, EventArgs e)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string path1 = path.Replace(@"\bin\Debug\", @"\Pics\");

           if (kast1 == 1)
           {
               this.pictureBox_terning1.Image = new Bitmap(path1 + "terning1.png"); 
           }
           //rest of the code
        }

And if you dont like replacing then set your pictures to be copied to bin directory by

right clicking on image -> properties -> Copy to output directory -> copy always

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM