c# - Convert System.Windows.Media.ImageSource to ByteArray -


is there way convert imagesource object byte array? have imagesource object bound wpf window, can convert byte array data base , convert imagesource can't reverse way.

thx in advance.

edit: tried convert imagesource bitmapimage got null object.

even if imagesource not bitmapimage may still cast bitmapsource, base class of wpf bitmap classes bitmapimage, bitmapframe, writeablebitmap, rendertargetbitmap etc. (see here).

so in case imagesource bitmapsource (and not drawingimage or d3dimage), following method converts byte array using specified bitmapencoder (e.g. pngbitmapencoder):

public byte[] imagesourcetobytes(bitmapencoder encoder, imagesource imagesource) {     byte[] bytes = null;     var bitmapsource = imagesource bitmapsource;      if (bitmapsource != null)     {         encoder.frames.add(bitmapframe.create(bitmapsource));          using (var stream = new memorystream())         {             encoder.save(stream);             bytes = stream.toarray();         }     }      return bytes; } 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -