Skip to main content

Microsoft Silverlight

Answered Question Setting image source through code?RSS Feed

(0)

Papasmurf0406
Papasmur...

Member

Member

3 points

8 Posts

Setting image source through code?

 Hello,

 I'm having my site create a products page dynamically--so we can add and delete products later and I won't have to recode those parts--and I've run into a slight problem with images: when I try to set the image source of my images, it says that I can't input the string address. So how do I do it?


Here is the code:

 
ProductImage(j) = New Image
ProductImage(j).Source = "sitemedia/background.jpg"

 

rajesh shirpuram
rajesh s...

Contributor

Contributor

2314 points

505 Posts

Re: Setting image source through code?

 use

image.SetSource( new URI(ImageSource));

Thanks
Rajesh Shirpuram

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Answered Question

Re: Setting image source through code?

 For a image located at remote

     MyImage.Source = new BitmapImage(new Uri("http://theURL/test.png"));

 For a image located near you in your page location

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

 

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

Papasmurf0406
Papasmur...

Member

Member

3 points

8 Posts

Re: Re: Setting image source through code?

Hey Steve,

when I enter New BitmapImage it says there is no such thing (the only it gives is bitconverter). Do I need to import something first? 

Papasmurf0406
Papasmur...

Member

Member

3 points

8 Posts

Re: Re: Re: Setting image source through code?

 Never mind, just figured it out :)

rajesh shirpuram
rajesh s...

Contributor

Contributor

2314 points

505 Posts

Re: Setting image source through code?

 Sorry i forgot that your are creating new image object...

 use

image.SetSource( new BitMapImage(new URI(ImageSource)));

Thanks
Rajesh Shirpuram

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Setting image source through code?

 Please Include the line at the top of C# code behind

using System.Windows.Media.Imaging;

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities