How do you set Image.Source in Silverlight(Code behind)

北战南征 提交于 2019-12-18 18:49:47

问题


I am dynamically generating an image through code-behind in Silverlight and apparently the image source doesn't accept a string or Uri as a path.

How can I set the source?


回答1:


How do you mean it won't accept a string as source?

Are you not able to do this?

Or are you saying your image is in memory and you don't know how to reference it?

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



回答2:


// 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));  



回答3:


I needed to replace the following to get the solution work:

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

MyNameSpace did not work for me, but the ExecutingAssemblyName did, so:

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


来源:https://stackoverflow.com/questions/577502/how-do-you-set-image-source-in-silverlightcode-behind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!