2009-02-23 73 views
30

我在Silverlight中通過代碼隱藏動態生成圖像,顯然圖像源不接受字符串或Uri作爲路徑。如何在Silverlight中設置Image.Source(後面的代碼)

如何設置來源?

+0

這花了我一點時間才弄清楚。關塔姆的答案看起來像我所用的。 – BenMaddox 2009-02-23 17:56:12

+0

雖然我不得不改變它,但它沒有在路徑中包含名稱空間 – Drahcir 2009-02-23 18:41:03

回答

53

你怎麼看它不接受一個字符串作爲源?

你無法做到這一點嗎?

或者你是說你的形象是在記憶中,你不知道如何引用它?

this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative)); 
+1

Does not接受字符串,我的意思是例如:MyImage.Source =「/ MyNameSpace; images/images.source.php」像在asp.net – Drahcir 2009-02-23 13:47:22

6
// create a new image 
Image image = new Image(); 

// better to keep this in a global config singleton 
string hostName = Application.Current.Host.Source.Host;     
if (Application.Current.Host.Source.Port != 80) 
    hostName += ":" + Application.Current.Host.Source.Port; 

// set the image source 
image.Source = new BitmapImage(new Uri("http://" + hostName + "/image111.jpg", UriKind.Absolute)); 
1

我需要替換以下得到解決工作:

this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;components/images/someimage.png", UriKind.Relative)); 

myNameSpace對象並沒有爲我工作,但ExecutingAssemblyName一樣,所以:

Dim tmp As String() = Assembly.GetExecutingAssembly.FullName.Split(","c) 
Dim path As String = "/" & tmp(0) & ";component/images/" 
MyImage.Source = new BitmapImage(new Uri(path & "someImage.png")) 
相關問題