0

我在控制台应用程序中制作了一个 yatzy 游戏,我目前正在尝试在表单应用程序中制作一个。
这是我到目前为止所拥有的:

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");
           }
       }


   }
}

如您所见,我在表单中定义了“kast1”等,根据结果,它应该在图片框中显示不同的图像。我查看了我能找到的所有帖子,所有的解决方案都大惊小怪。

我试过没有“这个”。我已经尝试过 "= image.FromFile("Pics\terning#.png");"
没有任何效果。

4

4 回答 4

1

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

于 2013-09-10T11:38:27.777 回答
1

不需要使用 Bitmap 对象来加载 PictureBox 中的图像。也许更简洁的加载图像的方法是使用Load Method of PictureBox.

如果我假设图像与您的应用程序位于同一目录中,并包含在另一个文件夹中,这将很容易完成这项工作;

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

Load方法需要一个指向图像的有效路径作为参数,因此无需Refresh在加载每个图像后调用。路径是绝对的还是相对的并不重要,重要的是路径应该指向一个图像。但我建议你像这样为你的可执行文件创建一个绝对路径;

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

正如您所提到的,即使没有遇到错误,图像也不会被加载,对于这种情况,调试将是查找程序运行异常的理想技术。

于 2013-09-10T12:09:19.110 回答
0

您要加载的图像很可能不在您正在查看的路径中。

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

预计您的图像位于 {youprojectfolder}\bin\DebugORRelease\Pics。

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

期望您的图像位于 {最可能的 c:}\Pics 中。

所以我建议先检查一下,如果这不起作用添加断点(F9)并开始调试(F5),请参阅 MSDN 以了解调试介绍

我还建议您将 if, else if 构造替换为switch

于 2013-09-10T11:43:30.063 回答
0

试试这个代码,我已经尝试过并且可以工作(它将获取您的应用程序的 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
        }

如果你不喜欢替换然后设置你的图片被复制到 bin 目录

右键单击图像-> 属性-> 复制到输出目录-> 始终复制

.

于 2013-09-10T11:45:39.377 回答