Silverlight: image to byte[]

前端 未结 3 1486
遇见更好的自我
遇见更好的自我 2020-12-03 13:08

I\'m able to convert a byte[] to an image:

byte[] myByteArray = ...;  // ByteArray to be converted

MemoryStream ms = new MemoryStream(my);
BitmapImage bi =          


        
3条回答
  •  醉酒成梦
    2020-12-03 13:30

        public static void Save(this BitmapSource bitmapSource, Stream stream)
        {
            var writeableBitmap = new WriteableBitmap(bitmapSource);
    
            for (int i = 0; i < writeableBitmap.Pixels.Length; i++)
            {
                int pixel = writeableBitmap.Pixels[i];
    
                byte[] bytes = BitConverter.GetBytes(pixel);
                Array.Reverse(bytes);
    
                stream.Write(bytes, 0, bytes.Length);
            }
        }
    
        public static void Load(this BitmapSource bitmapSource, byte[] bytes)
        {
            using (var stream = new MemoryStream(bytes))
            {
                bitmapSource.SetSource(stream);
            }
        }
    
        public static void Load(this BitmapSource bitmapSource, Stream stream)
        {
            bitmapSource.SetSource(stream);
        }
    

提交回复
热议问题