繁体   English   中英

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

[英]I cannot change image in picturebox c#

我在一个控制台应用程序中做了一个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“);”
什么都行不通。

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

没有必要仅使用Bitmap对象来加载PictureBox中的图像。也许更加清晰的加载图像的方法是使用Load Method of PictureBoxLoad 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); 

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

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

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

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

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

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

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

我还建议你用开关替换你的if,else if构造。

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

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

暂无
暂无

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

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