locked
No imaging component suitable to complete this operation was found.' RRS feed

  • Question

  • Hi I am getting this exception , when I try to bind a listview with a List<MyImage> type

    "No imaging component suitable to complete this operation was found."

    occurs for the target ListView and Binding Expression Path = AllImages

    Please see attached code

    Please let me know how to fix this


    <DataTemplate x:Key="BrowsePictureTemplate" DataType="{x:Type loc:MainContent}">
                <Grid Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.2*"/>
                        <RowDefinition/>    
                        <RowDefinition Height="0.3*"/>
                        <RowDefinition Height="0.2*"/>
                    </Grid.RowDefinitions>
                    <Image Grid.RowSpan="3" Grid.Row="0" Source="{Binding ElementName=ImageList,Path=SelectedItem.Image}">
     
                    </Image>
                    <ListView Grid.Row="2" ItemsSource="{Binding AllImages}" Name="ImageList" Background="Gray" Opacity="0.7" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"></StackPanel>
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Opacity="1">
                                    <Image Source="{Binding Image}" Width="100" Height="100" Opacity="1"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
     
                    <StackPanel Grid.Row="3" Background="Black">
     
                    </StackPanel>
                    <StackPanel Grid.Row="0" Background="Gray" Opacity="0.7" >
                        <TextBlock  Text="Awesome" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
     
     
    ////Code Behind C#///
     
    public class MainContent
        {
      public List<MyImage> AllImages
            {
                get 
                {
                    List<MyImage> result = new List<MyImage>();
                    foreach (string filename in System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))
                    {
                        try
                        {
                            result.Add(new MyImage(new BitmapImage(new Uri(filename)), System.IO.Path.GetFileNameWithoutExtension(filename)));
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(ex.Message);
                        }
     
                    }
                    return result;
                }
               
            }
     
            /// <summary>
            /// Class for MyImage Type
            /// </summary>
     
            public class MyImage
            {
     
                public MyImage(ImageSource image, string name)
                {
                    _image = image;
                    _name = name;
                }
     
                public override string ToString()
                {
                    return _name;
                }
                private ImageSource _image;
                public ImageSource Image { get { return _image; } }
     
                private string _name;
                public string Name { get { return _name; } }
     
            }
     
    }

    ctrlnick
    Wednesday, May 13, 2009 4:11 PM

Answers

  • I think you forget to check the file type of all the files in your ''MyPictures' folder.

    There might be some un-image files such as: Desktop.ini, Sample Pictures.lnk, Thumbs.db

    Before you create instance of MyImage, you should make sure you put in an real image.

    Below is a simple checking solution based on the filename, hope it helps.

    For example:
       public class MainContent
        {
            private List<MyImage> _AllImage;
            public List<MyImage> AllImages
            {
                get
                {
                    if (_AllImage == null)
                    {
                        _AllImage = new List<MyImage>();
                        foreach (string filename in System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))
                        {
                            try
                            {
                                if (IsImage(filename))
                                {
                                    MyImage mi = new MyImage(filename);
                                    if (mi != null)
                                    {
                                        _AllImage.Add(new MyImage(filename));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                    }
                    return _AllImage;
                }
            }
            public static string GetFileExt(string name)
            {
                return name.Substring(name.LastIndexOf(".") + 1);
            }
            public static bool IsImage( string name )
            {
                string ext = GetFileExt(name);
                switch (ext.ToLower())
                {
                    case "jpg":
                    case "jpeg":
                        return true;
                    case "bmp":
                        return true;
                    case "png":
                        return true;
                    case "gif":
                        return true;
                    //...... add more
                    default:
                        return false;
                }
            }
        }
        public class MyImage
        {
            public MyImage( string filename )
            {
                try
                {
                    //Check the file is JPG,BMP...
                    _image = new BitmapImage();
                    _image.BeginInit();
                    _image.UriSource = new Uri(filename);
                    _image.EndInit();
                    _name = filename;
                }
                catch
                { 
                }
            }
            public override string ToString()
            {
                return _name;
            }
            private BitmapImage _image;
            public BitmapImage Image { get { return _image; } }
            private string _name;
            public string Name { get { return _name; } }
        }
    • Proposed as answer by Tao Liang Tuesday, May 19, 2009 5:39 AM
    • Marked as answer by Tao Liang Wednesday, May 20, 2009 1:33 AM
    Tuesday, May 19, 2009 5:38 AM

All replies

  • Have you tried just displaying these images more directly in a WPF app?  Can you open the images in other image editing programs?  This error is returned by the underlying Windows Imaging Component - it typically means that there was a problem reading your image file, i.e. it might be corrupted.  Here's another related thread: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c28ebaa5-862f-4332-a5b3-8d7032bc24da/
    Wednesday, May 13, 2009 9:03 PM
  • Hi Brendan .. .I did try it as a seperate project... before putting it into a datatemplate in my original project... I have many datatemplates and I toggle between them to show on the screen

    When tried seprately as a WPF app , It has no issues.. 

    It runs very smoothly.. I will attach code for the WPF app  also

    Please suggest me thebest

    <Window x:Class="MyPhotoBrowser.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
       
        <Grid Height="Auto" Width="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.2*"/>
                <RowDefinition/>
                <RowDefinition Height="0.3*"/>
                <RowDefinition Height="0.2*"/>
            </Grid.RowDefinitions>
            <Image Grid.RowSpan="3" Grid.Row="0" Source="{Binding ElementName=ImageList,Path=SelectedItem.Image}">
    
            </Image>
            <ListView Grid.Row="2" ItemsSource="{Binding AllImages}" Name="ImageList" Background="Gray" Opacity="0.7" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"></StackPanel>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Opacity="1">
                            <Image Source="{Binding Image}" Width="100" Height="100" Opacity="1"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
            <StackPanel Grid.Row="3" Background="Black">
    
            </StackPanel>
            <StackPanel Grid.Row="0" Background="Gray" Opacity="0.7" >
                <TextBlock  Text="Awesome" />
            </StackPanel>
    
        </Grid>
    </Window>
    
    
    /// CSHARP////
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace MyPhotoBrowser
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : System.Windows.Window
        {
    
            public Window1()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            public class MyImage
            {
                private ImageSource _image;
                private string _name;
    
                public MyImage(ImageSource image, string name)
                {
                    _image = image;
                    _name = name;
                }
    
                public override string ToString()
                {
                    return _name;
                }
    
                public ImageSource Image { get { return _image; } }
                public string Name { get { return _name; } }
            }
    
            public List<MyImage> AllImages
            {
                get
                {
                    List<MyImage> result = new List<MyImage>();
                    foreach (string filename in
                        System.IO.Directory.GetFiles(
                        Environment.GetFolderPath(
                        Environment.SpecialFolder.MyPictures)))
                    {
                        try
                        {
                            result.Add(
                                new MyImage(
                                new BitmapImage(
                                new Uri(filename)),
                                System.IO.Path.GetFileNameWithoutExtension(filename)));
                        }
                        catch { }
                    }
                    return result;
                }
            }
    
        }
    }
    
    

    ctrlnick
    Wednesday, May 13, 2009 9:55 PM
  • This is the Complete error from the Output Window

    System.Windows.Data Error: 16 : Cannot get 'AllImages' value (type 'List`1') from '' (type 'MainContent'). BindingExpression:Path=AllImages; DataItem='MainContent' (HashCode=19082427); target element is 'ListView' (Name='ImageList'); target property is 'ItemsSource' (type 'IEnumerable') TargetInvocationException:'System.Reflection.TargetInvocationException: Property accessor 'AllImages' on object 'SomeApp.MainContent' threw the following exception:'No imaging component suitable to complete this operation was found.' ---> System.Exception: No imaging component suitable to complete this operation was found.
       at SomeApp.MainContent.get_AllImages()


    ctrlnick
    Wednesday, May 13, 2009 10:01 PM
  • Can you paste the full version code of the project having issue. (XAML + C#)

    Or send the full version of your project to my email:
    liangtom@gmail.com


    Monday, May 18, 2009 7:40 AM
  • I think you forget to check the file type of all the files in your ''MyPictures' folder.

    There might be some un-image files such as: Desktop.ini, Sample Pictures.lnk, Thumbs.db

    Before you create instance of MyImage, you should make sure you put in an real image.

    Below is a simple checking solution based on the filename, hope it helps.

    For example:
       public class MainContent
        {
            private List<MyImage> _AllImage;
            public List<MyImage> AllImages
            {
                get
                {
                    if (_AllImage == null)
                    {
                        _AllImage = new List<MyImage>();
                        foreach (string filename in System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))
                        {
                            try
                            {
                                if (IsImage(filename))
                                {
                                    MyImage mi = new MyImage(filename);
                                    if (mi != null)
                                    {
                                        _AllImage.Add(new MyImage(filename));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                    }
                    return _AllImage;
                }
            }
            public static string GetFileExt(string name)
            {
                return name.Substring(name.LastIndexOf(".") + 1);
            }
            public static bool IsImage( string name )
            {
                string ext = GetFileExt(name);
                switch (ext.ToLower())
                {
                    case "jpg":
                    case "jpeg":
                        return true;
                    case "bmp":
                        return true;
                    case "png":
                        return true;
                    case "gif":
                        return true;
                    //...... add more
                    default:
                        return false;
                }
            }
        }
        public class MyImage
        {
            public MyImage( string filename )
            {
                try
                {
                    //Check the file is JPG,BMP...
                    _image = new BitmapImage();
                    _image.BeginInit();
                    _image.UriSource = new Uri(filename);
                    _image.EndInit();
                    _name = filename;
                }
                catch
                { 
                }
            }
            public override string ToString()
            {
                return _name;
            }
            private BitmapImage _image;
            public BitmapImage Image { get { return _image; } }
            private string _name;
            public string Name { get { return _name; } }
        }
    • Proposed as answer by Tao Liang Tuesday, May 19, 2009 5:39 AM
    • Marked as answer by Tao Liang Wednesday, May 20, 2009 1:33 AM
    Tuesday, May 19, 2009 5:38 AM
  • Sure I will Send you email ,

    Please respond
    ctrlnick
    Thursday, May 21, 2009 4:28 PM
  • Buddy  , That was so perfect ... It worked Like a Charm !!!
    ctrlnick
    Thursday, May 21, 2009 7:07 PM
  • Can you post the solution? Thanks
    Tuesday, February 23, 2010 6:51 PM