2012-07-03 127 views
3

我有1個圖片框,名爲studPic。我想得到的是,當我點擊「隨機播放」按鈕時,從資源中獲取隨機圖像。從按鈕點擊資源中隨機播放隨機圖片

private void button2_Click(object sender, EventArgs e) 
     { 
...  
     } 

研究之後,我發現以下

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/38d36fa4-1f5f-43e9-80f6-3c9a4edc7705/

我新手,C#..有更簡單的方法來實現這一結果?例如,不添加圖片名稱?

UPDATE

List<string> pictureNames = new List<string>(); 
pictureNames.Add("1"); 
pictureNames.Add("2"); 
pictureNames.Add("3"); 
int randomPictureIndex = new Random().Next(0, pictureNames.Count); 
string randomPictureName = pictureNames[randomPictureIndex]; 
pictureNames.Remove(randomPictureName); 
Image img = Properties.Resources.randomPictureName; //erroor 
studPic.Image = img; 

收到錯誤消息 錯誤1 'Properties.Resources' 不包含定義 'randomPictureName'

+0

什麼是你的資源? –

+0

@ChrisGessler應用程序資源 – heron

+0

@ChrisGessler已更新。請看看 – heron

回答

3

我不會用系統資源這一點。它們不像斷開連接的軟件的文件系統那樣可維護。

將您的圖片放在您的應用的文件夾中。這樣他們可以很容易地更新/更改。 說:

C:\Ninjas - app 
c:\Ninjas\images - images 

創建保存這些圖像的陣列。

string[] files = System.IO.Directory.GetFiles("c:\ninjas\images"); 

您需要在GetFiles上放置一些過濾器,以確保您只能獲取圖片。

現在抓住該數組中的一個隨機位置(您已經向您展示瞭如何做隨機數)。

我們有數組,讓我們將它洗,然後你可以依次通過他們去(這樣的速度比隨機挑選的CPU一定會喜歡你的。)

private string[] files; 
    private int currentIndex = 0; 

    private void initializeImages() 
    { 
     //Grab directories in your images directory 
     string appRoot = System.IO.Path.GetDirectoryName(Application.ExecutablePath); 
     files = System.IO.Directory.GetFiles(appRoot + @"\images"); 
     Random rnd = new Random(); 
     files = files.OrderBy(x => rnd.Next()).ToArray(); 
    } 


    private void setImage() 
    { 
     pictureBox1.ImageLocation = files[currentIndex]; 
    } 

    private void previousImage() 
    { 
     currentIndex = currentIndex > 0 ? currentIndex - 1 : 0; 
     setImage(); 
    } 

    private void nextImage() 
    { 
     currentIndex = currentIndex < files.Length - 1 ? currentIndex + 1 : files.Length - 1; 
     setImage(); 
    } 

有兩件事情:

  • 不要硬編碼文件路徑。在你的app.config文件中有這個,並引用它。
  • 您可以將文件數組設置爲全局,因此無需每次都運行它。

如果你想有這樣的作爲幻燈片放映運行,直至用戶取消它,我建議如下:

  • 使用調用的方法,以提高圖像計數的定時器對象,這改變了圖片。
  • 不要在GUI上使用thread.sleep,因爲它會暫停GUI - 這不是一件好事。

如果你想添加一個/上一個按鈕,你需要有一個全球指數(說CURRENTINDEX),可以增加/減少,然後調用代碼來設置圖像

1

一些設置是參與你的部分,但反對者大多是正確的。對於生產應用來說,這不是一個非常有效的解決方案。話雖如此,我懷疑這是你分發給很多人的東西,所以我們稱之爲學術練習。如果您只是簡單地將資源添加到您的應用程序中,並將其命名爲「ImageResource」(任何名稱都可以),然後將圖像添加到它,則可以使用以下代碼(假設存在相應的UI元素)。

首先,我們創建一個函數來從資源中提取位圖。

private Bitmap[] GetResourceImages() 
{ 
    PropertyInfo[] props = typeof(ImageResource).GetProperties(BindingFlags.NonPublic | BindingFlags.Static); 
    var images = props.Where(prop => prop.PropertyType == typeof(Bitmap)).Select(prop => prop.GetValue(null, null) as Bitmap).ToArray(); 

    return images; 
} 

其次,我們創建將隨機基於可用的圖像圖像的功能:

private void RandomizePicture() 
{ 
    Bitmap[] images = GetResourceImages(); 
    if (images == null || images.Length == 0) 
    { 
     //Nothing to do here... 
     return; 
    } 

    int maxValue = images.Length; 
    Random r = new Random(); 
    int idx = r.Next(maxValue); 
    this.uxStupidPic.Image = images[idx]; 
} 

最後,調用上的按鈕,點擊該功能:

private void btnRandmoize_Click(object sender, EventArgs e) 
{ 
    this.RandomizePicture(); 
} 

瞧,從資源文件隨機化圖像。快樂的編碼!

編輯:只是注意到你說你使用應用程序資源,而不是隨機資源文件。只需在GetResourceImages中將「ImageResource」替換爲「Properties.Resources」,即可開始業務。

0

我喜歡Ryan Ternier的回答非常簡單和優雅,我打算髮布類似於他的解決方案。

我只是想對原因發表評論,爲什麼行不/不應該工作:

的圖像img = Properties.Resources.randomPictureName;

  1. 是因爲Properties Resources正在尋找名爲「randomPictureName」的資源而不是實際的名稱本身。
  2. Properties.Resources對象的工作方式是在代碼編譯之後,它會在運行時查找解決方案屬性中的所有資源。它不會按照你想要的方式工作,因爲你試圖傳入一個靜態變量,這個靜態變量甚至可能不存在於資源中,迫使你圍繞try/catch語句包裝你的方法以防萬一。

http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx