How to convert image to byte array

前端 未结 12 1423
悲哀的现实
悲哀的现实 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:45

    To be convert the image to byte array.The code is give below.

    public byte[] ImageToByteArray(System.Drawing.Image images)
    {
       using (var _memorystream = new MemoryStream())
       {
          images.Save(_memorystream ,images.RawFormat);
          return  _memorystream .ToArray();
       }
    }
    

    To be convert the Byte array to Image.The code is given below.The code is handle A Generic error occurred in GDI+ in Image Save.

    public void SaveImage(string base64String, string filepath)
    {
        // image convert to base64string is base64String 
        //File path is which path to save the image.
        var bytess = Convert.FromBase64String(base64String);
        using (var imageFile = new FileStream(filepath, FileMode.Create))
        {
            imageFile.Write(bytess, 0, bytess.Length);
            imageFile.Flush();
        }
    }
    

提交回复
热议问题