How do I convert a Bitmap to byte[]?

后端 未结 3 1712
囚心锁ツ
囚心锁ツ 2020-12-20 11:34

Basically I am inserting an image using the listviews inserting event, trying to resize an image from the fileupload control, and then save it in a SQL database using LINQ.<

3条回答
  •  一生所求
    2020-12-20 12:19

    Assuming, that your bitmap is bmp

    byte[] data;
    using(System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
       bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
       stream.Position = 0;
       data = new byte[stream.Length];
       stream.Read(data, 0, stream.Length);
       stream.Close();
    }
    

提交回复
热议问题