Skip to main content

Microsoft Silverlight

Answered Question Setting Image Source DynamicallyRSS Feed

(0)

krishnakiran.k
krishnak...

Member

Member

33 points

37 Posts

Setting Image Source Dynamically

I need to assign sources to the Images Dynamically at runtime.I get the Image sources by calling web service which returns me a array of strings which contains the sources.How could i do that.

Regards,

Krishna kiran 

pefc
pefc

Member

Member

100 points

49 Posts

Re: Setting Image Source Dynamically

Try this: 

YourImage.Source = new BitmapImage(new Uri(yourstring, UriKind.Relative));

krishnakiran.k
krishnak...

Member

Member

33 points

37 Posts

Re: Setting Image Source Dynamically

 Can i Give Local path of the local file to the image in a string format as a source to the Image.

such as @"C:\Documents and Settings\example.jpg" at run time

krishnakiran.k
krishnak...

Member

Member

33 points

37 Posts

Re: Setting Image Source Dynamically

Can i give Local file path to a Image such as @"C:\Documents and Settings\" in the source string  

robhouweling
robhouwe...

Contributor

Contributor

3158 points

540 Posts

Silverlight MVP

Re: Setting Image Source Dynamically

No, Silverlight runs in the browser, so c:\ is c:\ on the PC of the user viewing your site, not on your server.

(If this has answered your question, please click on mark as answer on this post)

Cheers!
Rob Houweling



My blog

gongdo
gongdo

Member

Member

98 points

37 Posts

Answered Question

Re: Setting Image Source Dynamically

You can't access local resources cuz security issues.
But, you can recieve local files from user by OpenFileDialog.

here is some sample codes that load image from local by using OpenFileDialog.
Copy this code to Page UserControl and try it :)

        public Page()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "All Images|*.jpg;*.png|JPEG Images(*.jpg)|*.jpg|PNG Images(*.png)|*.png";
            ofd.EnableMultipleSelection = false;
            DialogResult result = ofd.ShowDialog();
            if (result == DialogResult.OK)
            {
                FileDialogFileInfo file = ofd.SelectedFile;
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(file.OpenRead());    // <-- load bitmap image as stream.

                Image img = new Image();
                img.Source = bitmap;
                img.Stretch = Stretch.None;
                LayoutRoot.Children.Add(img);
            }
        }

Hope it helps.
--Gongdo

--Gongdo

Sopheap Ly
Sopheap Ly

Participant

Participant

902 points

205 Posts

Re: Re: Setting Image Source Dynamically

Is someone trying to create a virus here? :D

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities