我正在使用 DevExpress PictureEdit,我试图获取从以下代码加载的图像的字节
byte[] picBytes = picStudent.EditValue as byte[];
但它总是返回 null。怎么办?
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();
}
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