How to convert image to byte array

前端 未结 12 1582
悲哀的现实
悲哀的现实 2020-11-22 09:52

Can anybody suggest how I can convert an image to a byte array and vice versa?

I\'m developing a WPF application and using a stream reader.

12条回答
  •  长情又很酷
    2020-11-22 10:44

    If you don't reference the imageBytes to carry bytes in the stream, the method won't return anything. Make sure you reference imageBytes = m.ToArray();

        public static byte[] SerializeImage() {
            MemoryStream m;
            string PicPath = pathToImage";
    
            byte[] imageBytes;
            using (Image image = Image.FromFile(PicPath)) {
                
                using ( m = new MemoryStream()) {
    
                    image.Save(m, image.RawFormat);
                    imageBytes = new byte[m.Length];
                   //Very Important    
                   imageBytes = m.ToArray();
                    
                }//end using
            }//end using
    
            return imageBytes;
        }//SerializeImage
    

提交回复
热议问题