如何从PictureBox中获取字节数组?

问题描述 投票:0回答:2

我正在使用 DevExpress PictureEdit,我试图获取从以下代码加载的图像的字节

byte[] picBytes = picStudent.EditValue as byte[];

但它总是返回 null。怎么办?

c# devexpress
2个回答
0
投票

PictureEdit.EditValue 属性根本不包含字节数组。此属性包含

System.Drawing.Image
实例 (proof)。因此,要从 PictureEdit 获取图像字节,请使用以下方法:

Image img = pictureEdit1.EditValue as Image; // or use the PictureEdit.Image property
using(MemoryStream ms = new MemoryStream()) {
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    byte[] bytes = ms.ToArray();
}

0
投票

VB.NET 中的真实示例:

    public function Pic_to_Byte() as byte()    
       Dim img As System.Drawing.Image = TryCast(pic_Sample.EditValue, System.Drawing.Image) 
        If pic_Sample.EditValue = Nothing Then Exit Sub
        Using ms As New MemoryStream()
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            Dim fileByte As Byte() = ms.ToArray()
            return fileByte
        End Using
end function
© www.soinside.com 2019 - 2024. All rights reserved.