c# - wpf - why dont images display in the listbox even there are values of the images in code -
i need me problem experiencing with. have single listbox supposed display car detials (right side) , car image (left side) after retrieving them database. note: convert byte images database working fine. problem images dont display in listbox (left side) car details displayed. missing images! make sure car , carimage in wpf style binding. no idea have done wrong in code or wpf style. aprreciate if willing take @ codes problem. appreciated. thanks!
wpf:
<listbox style="{dynamicresource listboxstyle1}" displaymemberpath="car" x:name="listboxcars" />
wpf style - car information (right side) , car image (left side):
<datatemplate x:key="templatelistboxitem"> <grid margin="5"> <grid.columndefinitions> <columndefinition width="auto"></columndefinition> <columndefinition width="*"></columndefinition> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition></rowdefinition> <rowdefinition></rowdefinition> </grid.rowdefinitions> <border grid.column="0" grid.row="0" grid.rowspan="2" margin="0,0,10,0"> <!-- binding car image database --> <image source="{binding path=carimage}" stretch="fill" height="40" width="40"></image> </border> <!-- same here binding car --> <textblock text="{binding path=car}" fontweight="bold" grid.column="1" grid.row="0"></textblock> </grid> </datatemplate> <style x:key="listboxstyle1" targettype="{x:type listbox}"> <setter property="itemtemplate" value="{staticresource resourcekey=templatelistboxitem}"></setter> </style>
this method retrieve list of car informations own images database
public list<carinfo> getcarimageslist (int days) { cars = new list<carinfo>(); const string sqlquery = "select car, carimage cartemplates days = @days order car asc"; using (var command = new sqlcommand(sqlquery, sqlconn)) { try { command.commandtype = commandtype.text; command.parameters.addwithvalue("@days", days); using (var reader = command.executereader()) { while (reader.read()) { byte[] buffer = new byte[10000]; var car = new carinfo { car = reader["car"].tostring() }; if (!reader.isdbnull(reader.getordinal("carimage"))) { long size = reader.getbytes(reader.getordinal("carimage"), 0, buffer, 0, 10000); using (memorystream strm = new memorystream()) { strm.write(buffer, 0, (int) size); strm.position = 0; system.drawing.image img = system.drawing.image.fromstream(strm); car.carimage = img; } } cars.add(car); } } . . . return cars; }
last thing: when click button, listbox display carinformation (right side) , carimage (left side). in runtime check there list of carinformations , carimages in _databasecarlist. there value (byte) of images (list.carimage) why dont images display?
private void button1_click(object sender, selectionchangedeventargs e) { _carimagelist = new carimageextractor(); const int oneday = 1; var lstcar = new list<carinfo>(); _databasecarlist = new observablecollection<carinfo>(_carimagelist.getcarimageslist(oneday)); if (_databasecarlist != null) { foreach (var list in _databasecarlist) { lstcar.add(new carinfo{car = list.car, carimage = list.carimage}); } listboxcars.itemssource = lstcar; } }
you using windowsforms image (system.drawing.image) not support wpf imagesource image. (when debugging in visualstudio should binding errors imagesourceconverter)
for wpf need use bitmapimage.
change property carimage type system.windows.media.imaging.bitmapimage
, change initialization code of image following.
strm.write(buffer, 0, (int) size); strm.position = 0; bitmapimage img = new bitmapimage(); img.begininit(); img.streamsource= strm; img.endinit(); car.carimage = img;
Comments
Post a Comment