0

i try to show images from database in datagrid that i saved them as binary. pictures are showed but im getting binding errors. my datagrid xaml codes are:

<DataGridTemplateColumn Header=" Image" Width=" 95">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate >
                        <Image  Source="{Binding ProductImage ,TargetNullValue={x:Null}}" Width=" 80" Height=" 80"></Image>
                    </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle >
                        <Style TargetType="{x:Type DataGridColumnHeader }">
                            <Setter Property="FontFamily" Value="Arial Black"/>
                            <Setter Property="HorizontalContentAlignment"  Value="Center"/>
                            <Setter Property="Foreground"  Value="#FF3211B8" />
                            <Setter Property="FontSize"  Value="12"/>
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
            </DataGridTemplateColumn>

and the c# code that im using to convert images to binary and save them to database

FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                    byte[] ImgByteArr = new byte[fileStream.Length];
                    fileStream.Read(ImgByteArr, 0, ImgByteArr.Length);
                    fileStream.Close();

and i send (ImgByteArr) to database

and this is binding errors:

when i scroll up and down, number of binding errors are increasing. also image can be null.

Hossein
  • 17
  • 4
  • 1
    As error message indicated, you need to convert the byte array to `BitmapImage` or other classes which inherit `ImageSource`. https://stackoverflow.com/q/9564174/3137337 – emoacht Dec 28 '22 at 03:28
  • 1
    That is not true. The conversion from byte array to ImageSource is done automatically by a built-in TypeConverter (an ImageSourceConverter instance). You do not need to do it explicitly. Instead, the error message indicates that the implicit conversion fails because the byte array does not contain a valid bitmap frame. It is however unclear where exactly that error occurs when you say that images are actually shown. – Clemens Dec 28 '22 at 07:26
  • Also note that you can simplify the code to read an image file: `byte[] ImgByteArr = File.ReadAllBytes(FilePath);` – Clemens Dec 28 '22 at 08:37

0 Answers0