Not logged in. · Lost password · Register
Forum: MatriX and XmppDotNet RSS
Avatar
goodzhengcheng #1
Member since Mar 2013 · 8 posts
Group memberships: Members
Show profile · Link to this post
Subject: Convert BitmapImage to byte[] or stream in WinRT
Because the type of Vcard.Photo.Binval is byte[], So I must convert BitmapImage to  byte[] or stream in WinRT. Who can help me?
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
I use this code in some of our WPF projects. Have not tested it on WinRT yet. Does it work for you?

  1. public static Byte[] ByteBufferFromImage(BitmapImage imageSource)
  2. {
  3.     var ms = new MemoryStream();
  4.     var pngEncoder = new PngBitmapEncoder();
  5.     pngEncoder.Frames.Add(BitmapFrame.Create(imageSource));
  6.     pngEncoder.Save(ms);
  7.    
  8.     return ms.GetBuffer();
  9. }
  10.  
  11. public static BitmapImage ImageFromBuffer(Byte[] bytes)
  12. {
  13.     var stream = new MemoryStream(bytes);
  14.     var image = new BitmapImage();
  15.     image.BeginInit();
  16.     image.StreamSource = stream;
  17.     image.EndInit();
  18.     return image;
  19. }
Avatar
goodzhengcheng #3
Member since Mar 2013 · 8 posts
Group memberships: Members
Show profile · Link to this post
It seems does not work,there is no PngBitmapEncoder class in WinRT,and there is no Create method in BitmapFrame class.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters: