簡體   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