Getter for a byte[] object returning an Image object
<p>I have a database with a property of Binary type. I'd like to be able to insert byte arrays into the database and pull them out and display images. The way I have my code set up I would really like to be able to convert my byte[] into an image in the getter method in the hope that, that will make the image display.</p>

<p>The code that I have that runs is:</p>

<pre><code>[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon { get; set; }
</code></pre>

<p>The problem though is that this displays "System.Byte[]" in my datagrid instead of an image.</p>

<p>In an attemt to display the image I changed my getter and setter methods to the following:</p>

<pre><code>[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description                                       = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon;
public Image _AgentLastContactedIcon
{
    get
    {
        MemoryStream ms = new MemoryStream(AgentLastContactedIcon);
        Image img = Image.FromStream(ms);
        return img;
    }
    set
    {
        ImageConverter converter = new ImageConverter();
        byte[] array = (byte[])converter.ConvertTo(value, typeof(byte[]));
        AgentLastContactedIcon = array;
    }
}
</code></pre>

<p>I am getting this error though:
"Attribute 'PropertyDefinition' is not valid on this declaration type. It is only valid on 'property, indexer' declarations."</p>

<p>Following the advice I found on another post on stack overflow, I moved "public byte[] agentLastContactedIcon;" above:</p>

<pre><code>public byte[] AgentLastContactedIcon;

[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]            
public Image _AgentLastContactedIcon
{
    get
    {
        MemoryStream ms = new MemoryStream(AgentLastContactedIcon);
        Image img = Image.FromStream(ms);
        return img;
    }
    set
    {
        ImageConverter converter = new ImageConverter();
        byte[] array = (byte[])converter.ConvertTo(value, typeof(byte[]));
        AgentLastContactedIcon = array;
    }
}
</code></pre>

<p>However doing so gave me the error: "The associated metadata type for type 'X' contains the following unknown properties or fields: _AgentLastContactedIcon. Please make sure that the names of these members match the names of the properties on the main type.</p>

<p>I'm using the following silverlight to display the items in the database:</p>

<pre><code>&lt;ctrls:CustomDataGrid
    x:Name="ComputersDataGrid"
    Grid.Row="1"
    Style="{StaticResource DataGridStyle}"
    ItemsSource="{Binding ItemsSource}"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    SelectionMode="{Binding SelectionMode}"
    PropertyDefinitions="{Binding PropertyDefinitions}" /&gt;
</code></pre>

<p>Thanks ahead of time for any help!</p>

<hr>

<p>Thank you for the great advice to implement a converter. I'm having a bit of trouble getting it working though. Im just trying here to convert a string to another string to see it work. Here is my sample converter: </p>

<pre><code>    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Globalization;
    using System.Windows.Data;

    namespace IAS.Shared.Web.Resources
    {
[ValueConversion(typeof(string), typeof(string))]
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        String str = (string)value;
        str = "changed";
        return str;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string str = (string)value;
        str = "changedBack";
        return str;
    }
}
</code></pre>

<p>}</p>

<p>I try to use it in my .xaml file here:</p>

<pre><code>    xmlns:converter="clr-namespace:AccessData.IAS.Shared.Web.Resources"
</code></pre>

<p>here:</p>

<pre><code>    &lt;UserControl.Resources&gt;
    &lt;converter:ImageConverter x:Key="ImageConverter" /&gt;
</code></pre>

<p>and here:</p>

<pre><code>    &lt;ctrls:CustomDataGrid x:Name="ComputersDataGrid"
                       Grid.Row="1"
                       Style="{StaticResource DataGridStyle}"
                       ItemsSource="{Binding ItemsSource, Converter={StaticResource ImageConverter}}"
                       SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                       SelectionMode="{Binding SelectionMode}"
                       PropertyDefinitions="{Binding PropertyDefinitions}"&gt;
</code></pre>

<p>My string doesn't change though. I am also getting the buid errors "The type 'converter:ImageConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built" and "The tag 'ImageConverter' does not exist in XML namespace 'clr-namespace:IAS.Shared.Wen.Resources'". However, if I build again, it runs. Do you see any issues with my code that may be preventing the converter from changing the string? Thanks, again!</p>
