BitmapImage: Accessing closed StreamSource

后端 未结 1 428
后悔当初
后悔当初 2021-01-24 22:01

I\'m using the following code trying to convert my BitmapImage to a byte[] so I can save it in my MS SQL Database.

    public static byte[] BufferFromImage(Bi         


        
相关标签:
1条回答
  • 2021-01-24 22:28

    I finally managed to get it working:

        public static byte[] BufferFromImage(BitmapImage img)
        {
            byte[] result = null;
    
            if (img != null)
            {
                using(MemoryStream memStream = new MemoryStream())
                {
                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(img));
                    encoder.Save(memStream);
    
                    result = memStream.ToArray();
                }
    
            }
    
            return result;
        }
    
    0 讨论(0)
提交回复
热议问题