简体   繁体   English

我不能改变图片框中的图像c#

[英]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. 我在一个控制台应用程序中做了一个yatzy游戏,我目前正在尝试在表单应用程序中创建一个。
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. 正如您所看到的,我在表单中定义了“kast1”等,并且根据结果,它应该在图片框中显示不同的图像。 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");" 我试过“= 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 . 没有必要仅使用Bitmap对象来加载PictureBox中的图像。也许更加清晰的加载图像的方法是使用Load Method of PictureBoxLoad 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. Load方法需要一个指向图像作为参数的有效路径,并且在加载每个图像后无需调用Refresh无论路径是绝对路径还是相对路径都没关系,但重要的是路径应指向图像。 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. 预计您的图像位于{youprojectfolder} \\ bin \\ DebugORRelease \\ Pics中。

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

Expects that you images reside in {most likely c:}\\Pics. 期待您的图像位于{最有可能是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 所以我建议首先检查一下,如果这不起作用添加断点(F9)并开始调试(F5) 请参阅MSDN以获得调试介绍

I would also suggest you to replace your if, else if construct with a switch . 我还建议你用开关替换你的if,else if构造。

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): 试试这个代码,我已经尝试过并且可以工作(它将获取你的应用程序的bin目录,然后你可以用图像目录替换文件夹):

  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 如果您不喜欢更换,请将您的照片设置为复制到bin目录

right clicking on image -> properties -> Copy to output directory -> copy always 右键单击图像 - >属性 - >复制到输出目录 - >始终复制

.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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