linq - How to convert varBinary into image or video when retrieved from database in C# -
i using visual studio 2010, (desktop application) , using linq sql save image/video or audio files databse in datatype varbinary (max). can do... problem is, can't them , display them in xaml because can't converting part correct. here have far (though not working); private void bt_click (object sender, routedeventargs e) { databasedatacontext context = new databasedatacontext();
var imagevalue = s in context.images s.imageid == 2 select s.imagefile; value = imagevalue.single().tostring(); //convert string , taking down next method converted in image } public string value { get; set; } public object imagesource //taking http://stackoverflow.com/ { { bitmapimage image = new bitmapimage(); try { image.begininit(); image.cacheoption = bitmapcacheoption.onload; image.createoptions = bitmapcreateoptions.ignoreimagecache; image.urisource = new uri(value, urikind.absolute); image.endinit(); grid.children.add(image); } catch { return dependencyproperty.unsetvalue; } return image; } } i not sure if on correct track? , assuming video or audio quite similar methods?
since image stored in binary format in database, want "stream" image object leveraging memorystream object.
looking @ code, solution this:
bitmapimage bmpimage = new bitmapimage(); memorystream msimagestream = new memorystream(); msimagestream.write(value, 0, value.length); bmpcardimage.begininit(); bmpcardimage.streamsource = new memorystream(msimagestream.toarray()); bmpcardimage.endinit(); image.source = bmpcardimage;
Comments
Post a Comment