Image.Source from another Image.Source in Silverlight 2.0
Last post 05-09-2008 11:47 AM by aplocher. 2 replies.
Sort Posts:
03-16-2008 9:27 PM
Image.Source from another Image.Source in Silverlight 2.0

I have some code that was fine in SL 1.1 and can't for the life of me get it to work in 2.0.

 I have an image on a xaml page that has a static source pointing to a local file.

In code I create a new image control on the fly and set the source of the first to the source of the new with this:

newImage.Source=oldImage.Source;

In SL 2.0 it tells me that the "value does not fall within the expected range". I definitely have a valid ImageSource/BitMapImage.

 Both images are System.WIndows.Controls.Image.

 Even if I do this replacement with another existing image control (i.e. not creating the image control on the fly), I get this error.

Any ideas? I'm all out....

thanks

 Julie

jlermanvt

Joined on 05-07-2007
Vermont, USA
Posts 5
03-16-2008 10:08 PM
Marked as Answer
Re: Image.Source from another Image.Source in Silverlight 2.0

The solution to a problem is to post it on a forum, because 5 minutes later you will finally figure it out yourself!

 I don't understand why this is necessary, but it works:

BitmapImage bmi=(BitmapImage)oldImage.Source;

newImage.Source=new BitmapImage(bmi.UriSource);

go figure....

jlermanvt

Joined on 05-07-2007
Vermont, USA
Posts 5
05-09-2008 11:47 AM
Re: Image.Source from another Image.Source in Silverlight 2.0

I'm having the same problem and I tried the solution above but it doesn't seem to do anything for me.  My Image is being loaded from a Stream, instead of a URL.  Has anyone gotten this to work?  Here is the code I'm working with:

 

            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.EnableMultipleSelection = true;
            fileDialog.Filter = "Images (.jpg, .jpeg, .png)|*.jpg;*.jpeg;*.png";
            if (DialogResult.OK == fileDialog.ShowDialog())
            {
                try
                {
                    foreach (FileDialogFileInfo info in fileDialog.SelectedFiles)
                    {
                        BitmapImage bimg = new BitmapImage();
                        using (Stream str = info.OpenRead())
                        {
                            bimg.SetSource(str);
                        }

                        images.Add(bimg);

                        Image img = new Image();
                        img.Source = bimg;
                        img.Cursor = Cursors.Hand;
                        img.Margin = new Thickness(2);
                        img.MaxWidth = 107;

                        imgList.Items.Add(img);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                //txtImageCount.Text = imgList.Children.Count.ToString() + " Images";
            }

 

then...

        private void imgList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                Image img = ((Image)e.AddedItems[0]);
                BitmapImage bmp = (BitmapImage)img.Source;
                imgMain.Source = new BitmapImage(bmp.UriSource);
                imgMain.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
            }

            btnDelete.IsEnabled = true;
        }

 

No exceptions get thrown or anything, it just simply does nothing when the SelectionChanged event fires.  If I step through it, that code is being called but nothing happens.

Thanks!

aplocher

Joined on 01-25-2006
Posts 1