c# - WritableBitmap to ImageSource in MVVM -


end goal: print 2d barcode using wpf , mvvm:

background info (probably not relevant) have 2 projects in solution. 1 project controls business logic, , second project controls printing logic, creation , printing of labels. using named pipes ipc. using mvvm , have xaml template design label , @ run time fill properties , print it. working correctly.

more info: (might relevant) using third party library creates 2d barcodes. working , call make barcode returns writable bitmap

issue: trying databind writable bitmap image control on template.

public void foobar(string[] labelproperties) {     try     {         barcodewriter writer = new barcodewriter()         {            format = barcodeformat.pdf_417,            options = new zxing.common.encodingoptions            {               height = 50,               width = 132,               margin = 0            }         };          var wb = writer.write("some string");          system.windows.controls.image newimage = new system.windows.controls.image()         {            height = 50,            horizontalalignment = horizontalalignment.left,            name = "image",            stretch = stretch.none,            verticalalignment = verticalalignment.top,            width = 132,            source = wb,         };          this.barcodeimage = newimage;     }     catch (exception e)     {         console.writeline(e.message.tostring().trim());     } } 

its worth noting cannot directly place writablebitmap barcodeimage.source

this.barcodeimage.source = wb; 

because i'm using mvvm design, barcodeimage isn't instantiated if try set source, throws null reference.

the xaml in template

<image height="50"         horizontalalignment="left"         margin="10,2,0,0"         name="image"         stretch="none"         verticalalignment="top"         width="132"         source="{binding lblbarcodeimage}" /> 

my thought because having instantiate new controls.image() , setting barcodeimage breaking somehow.

other things can show other classes , settup prove mvvm settup correctly, other controls databinding correctly - though strings databinding - no other image controls.

i have tried converting writablebitmap byte array , tried using solution, did not work

do not not create image control in code behind!

instead, declare view model property of type imagesource , in xaml bind image control's source property view model property.

view model:

public class yourviewmodel : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      private imagesource barcodeimage;      public imagesource barcodeimage     {         { return barcodeimage; }         set         {             barcodeimage = value;             propertychanged?.invoke(this, new propertychangedeventargs("barcodeimage"));         }     }      ... } 

xaml:

<image ... source="{binding barcodeimage}"/> 

in code behind, assign writeablebitmap barcodeimage property:

yourviewmodel.barcodeimage = wb; 

Comments