c# - Getter for a byte[] object returning an Image object -
i have database property of binary type. i'd able insert byte arrays database , pull them out , display images. way have code set able convert byte[] image in getter method in hope that, make image display.
the code have 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 displays "system.byte[]" in datagrid instead of image.
in attemt display image changed getter , setter methods 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 { { 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 getting error though: "attribute 'propertydefinition' not valid on declaration type. valid on 'property, indexer' declarations."
following advice found on post on stack overflow, 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 { { 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 gave me error: "the associated metadata type type 'x' contains following unknown properties or fields: _agentlastcontactedicon. please make sure names of these members match names of properties on main type.
i'm using following silverlight display items in database:
<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}" />
thanks ahead of time help!
thank great advice implement converter. i'm having bit of trouble getting working though. im trying here convert string string see work. here 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 use in .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. getting buid errors "the type 'converter:imageconverter' not found. verify not missing assembly reference , referenced assemblies have been built" , "the tag 'imageconverter' not exist in xml namespace 'clr-namespace:ias.shared.wen.resources'". however, if build again, runs. see issues code may preventing converter changing string? thanks, again!
converting byte[]
image
inside of getter bad idea. may costly operation , properties meant relatively simple, pass-through style access perhaps error checking or internal state management.
instead, how defining simple converter job you? can bind byte[]
property, use converter convert image.
Comments
Post a Comment