如果我的pictureboxstupic 包含图像,我在我的一个项目中有一个方法如下返回字节[]。虽然我想以这样一种方式转换它,如果pictureboxstupic中没有图片,它应该返回string =“NULL”,否则它应该返回bute []。我怎样才能做到这一点??
private byte[] GetPic()
{
using (var stream = new MemoryStream())
{
var bmp = new Bitmap(pictureboxstupic.Image);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
var data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
return data;
}
}
注意:我插入图像的列如下
CREATE TABLE [dbo].[Students]
([Photo] [image] NULL)
在我目前的情况下,我将图像插入到我上面提到的列中,即 Student.Photo 像这样
if (pictureboxstupic.Image == null)
{
cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = "NULL";
}
else
{
cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = GetPic();
}