我擁有一個Binary類型屬性的數據庫。我想能夠插入字節數組到數據庫中,並將其拉出並顯示圖像。我有我的代碼設置的方式我真的想能夠將我的byte []轉換爲getter方法中的圖像,希望能夠顯示圖像。getter返回一個Image對象的byte []對象
,我有一個運行的代碼是:
[PropertyDefinition("0B79C12F-21F4-4ECA-BF4F-E694B9DE73DB")]
[Display(Name = "Computer_AgentLastContactedIcon", Description = "Computer_AgentLastContactedIcon_Desc", Order = 5, ResourceType = typeof(EntityMetadataStrings))]
public byte[] AgentLastContactedIcon { get; set; }
的問題是,雖然這顯示「System.Byte []」在我的DataGrid,而不是圖像。
在attemt顯示圖像我改變了我的getter和setter方法如下:
[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;
}
}
我雖然收到此錯誤: 「屬性‘PropertyDefinition’不在此聲明類型有效。它只對'財產,索引'聲明有效。「
根據我在堆棧溢出的另一篇文章中發現的建議,我移動了「public byte [] agentLastContactedIcon;」以上:
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;
}
}
但是這樣做給我的錯誤:「類型相關的元數據類型‘X’包含以下未知屬性或字段:_AgentLastContactedIcon請確保這些成員的名字相匹配的名字。主類型的屬性
我用下面的Silverlight,以顯示在數據庫中的項目:
<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}" />
感謝提前任何幫助
非常感謝您提供實現轉換器的偉大建議。儘管如此,我卻遇到了一些麻煩。我只是想在這裏將一個字符串轉換爲另一個字符串來查看它的工作。這裏是我的樣品轉換器:
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;
}
}
}
我嘗試在我的.xaml文件這裏使用:
xmlns:converter="clr-namespace:AccessData.IAS.Shared.Web.Resources"
這裏:
<UserControl.Resources>
<converter:ImageConverter x:Key="ImageConverter" />
這裏:
<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}">
我的字符串雖然沒有改變。我也遇到了buid錯誤「找不到類型'轉換器:ImageConverter'驗證你沒有丟失一個程序集引用,並且所有引用的程序集都已經構建好了」和「標記'ImageConverter'不存在於XML名稱空間中'CLR命名空間:IAS.Shared.Wen.Resources'」。但是,如果我再次構建,它會運行。你看到我的代碼可能會阻止轉換器更改字符串的任何問題嗎?再次感謝!
FCL本身是否包含一個類似的接口來實現'IValueConverter? – abatishchev