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.
The code that I have that runs is:
[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon { get; set; }
The problem though is that this displays "System.Byte[]" in my datagrid instead of an image.
In an attemt to display the image I changed my getter and setter methods to the following:
[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;
}
}
I am getting this error though: "Attribute 'PropertyDefinition' is not valid on this declaration type. It is only valid on 'property, indexer' declarations."
Following the advice I found on another post on stack overflow, I moved "public byte[] agentLastContactedIcon;" above:
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;
}
}
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.
I'm using the following silverlight to display the items in the database:
<ctrls:CustomDataGrid
x:Name="ComputersDataGrid"
Grid.Row="1"
Style="{StaticResource DataGridStyle}"
ItemsSource="{Binding ItemsSource}"
开发者_JS百科 SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
SelectionMode="{Binding SelectionMode}"
PropertyDefinitions="{Binding PropertyDefinitions}" />
Thanks ahead of time for any help!
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:
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;
}
}
}
I try to use it in my .xaml file here:
xmlns:converter="clr-namespace:AccessData.IAS.Shared.Web.Resources"
here:
<UserControl.Resources>
<converter:ImageConverter x:Key="ImageConverter" />
and here:
<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}">
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!
Converting a byte[]
to an Image
inside of a getter is a bad idea. It may be a costly operation and properties are meant for relatively simple, pass-through style access with perhaps some error checking or internal state management.
Instead, how about defining a simple Converter to do the job for you? You can bind to the byte[]
property, but use a converter to convert that to an image.
精彩评论