Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
What's wrong with this picture?
 
http://gilgameshcontrite.com/BadImage.jpg[^]
 
Instead of displaying a nice picture of a prehistoric plant, the string of the location of the bitmap is being displayed!
 
Here's the XAML (snippet):
 
<DataTemplate x:Key="YoungPicCell">
    <StackPanel Orientation="Horizontal">
        <Image Height="200" Width="200" Stretch="None"  Source="{Binding YoungPicBmp}"    />
    </StackPanel>
</DataTemplate>
 

The filenames (and other data) are loaded at runtime from an XML file.
 
Here is the relevant code:
 
      public WindowViewModel _wvm;
 
        public PlantDisplay()
        {
            InitializeComponent();
            _wvm = new WindowViewModel();
            this.DataContext = _wvm;
        }
 
        public class LVData
        {
            public string Name { get; set; }
            public string YoungPic { get; set; }
            public BitmapSource YoungPicBmp { get { return new BitmapImage(new Uri("{YoungPic}")); } }
            public string MediumPic { get; set; }
            public BitmapSource MediumPicBmp { get { return new BitmapImage(new Uri("{MediumPic}")); } }
            public string AdultPic { get; set; }
            public BitmapSource AdultPicBmp { get { return new BitmapImage(new Uri("{AdultPic}")); } }
            public bool SaltWater { get; set; }
            public bool FreshWater { get; set; }
            public bool Grasslands { get; set; }
            public bool Swamp { get; set; }
            public bool TropicalForest { get; set; }
            public bool Forest { get; set; }
            public bool ForestEdge { get; set; }
            public bool Sand { get; set; }
            public bool Coastal { get; set; }
            public bool RiverBorder { get; set; }
            public bool LakeBorder { get; set; }
            public bool Floodplain { get; set; }
        }
 

        public class WindowViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            //called when a property is changed
            protected void RaisePropertyChanged(string PropertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
 
            private ObservableCollection<LVData> _plantList = new ObservableCollection<LVData>();
            public ObservableCollection<LVData> lsvData
            {
                get { return _plantList; }
                set { _plantList = value; RaisePropertyChanged("lsvData"); }
            }
 
            public void PopulateDataFromXML(string filename)
            {
 
                XDocument loaded = XDocument.Load(filename);
 
                var Plants = from x in loaded.Descendants("Plants")
                              select new
                              {
                                  Name = x.Descendants("Name").First().Value,
                                  YoungPic = x.Descendants("YoungPic").First().Value,
                                  MediumPic = x.Descendants("MediumPic").First().Value,
                                  AdultPic = x.Descendants("AdultPic").First().Value,
                                  SaltWater = x.Descendants("SaltWater").First().Value,
                                  FreshWater = x.Descendants("FreshWater").First().Value, 
                                  Grasslands = x.Descendants("Grasslands").First().Value, 
                                  Swamp = x.Descendants("Swamp").First().Value, 
                                  TropicalForest = x.Descendants("TropicalForest").First().Value, 
                                  Forest = x.Descendants("Forest").First().Value, 
                                  ForestEdge = x.Descendants("ForestEdge").First().Value, 
                                  Sand = x.Descendants("Sand").First().Value, 
                                  Coastal = x.Descendants("Coastal").First().Value,
                                  RiverBorder = x.Descendants("RiverBorder").First().Value,
                                  LakeBorder = x.Descendants("LakeBorder").First().Value,
                                  Floodplain = x.Descendants("Floodplain").First().Value
                              };
 

                foreach (var _plant in Plants)
                {
                    _plantList.Add(new LVData { 
                        Name = _plant.Name,
                        YoungPic = _plant.YoungPic, 
                        MediumPic = _plant.MediumPic, 
                        AdultPic = _plant.AdultPic, 
                        SaltWater = Convert.ToBoolean(_plant.SaltWater),
                        FreshWater = Convert.ToBoolean(_plant.FreshWater),
                        Grasslands = Convert.ToBoolean(_plant.Grasslands),
                        Swamp = Convert.ToBoolean(_plant.Swamp),
                        TropicalForest = Convert.ToBoolean(_plant.TropicalForest),
                        Forest = Convert.ToBoolean(_plant.Forest),
                        Sand = Convert.ToBoolean(_plant.Sand),
                        Coastal = Convert.ToBoolean(_plant.Coastal),
                        RiverBorder = Convert.ToBoolean(_plant.RiverBorder),
                        LakeBorder = Convert.ToBoolean(_plant.LakeBorder),
                        Floodplain = Convert.ToBoolean(_plant.Floodplain) 
                         
                    });
 
                }
                RaisePropertyChanged("lsvData");
            }
 
  
}
Posted 13-Jun-13 8:05am
Zetar379
Edited 13-Jun-13 8:07am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use converter for the same .Please have a look of following
 
<Window
xmlns:local1="clr-namespace:**********">
</window>
 

<Window.Resources>
<local1:UriToBitmapConverter x:Key="Imageconverter"></local1:UriToBitmapConverter>
</Window.Resources>
 
<Image Height="200" Width="200" Stretch="None"  Source="{Binding FrameBrdr, Converter={StaticResource Imageconverter}}"  />
 

need to create a converter with following code :- 
 
public class UriToBitmapConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                BitmapImage bi = new BitmapImage();
                if (value != null)
                {
                    using (FileStream fileStream = File.OpenRead(value.ToString()))
                    {
 
                        MemoryStream ms = new MemoryStream();
                        fileStream.CopyTo(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        fileStream.Close();
                        bi.BeginInit();
                        bi.CacheOption = BitmapCacheOption.OnLoad;
                        bi.StreamSource = ms;
                        bi.EndInit();
                        bi.Freeze();
 
                    }
                }
                else
                {
                    bi = new BitmapImage();
                }
 
                return bi;
            }
            catch (Exception ex)
            {
               return null;
            }
            finally
            {
                GC.AddMemoryPressure(10000);
                GC.Collect();
            }
 
}
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
                    }
    }
 

 

create this class under the same namespace as ur mainclass have
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100