下面的代码是我的照片选择选择器(我项目中的另一个按钮就是将picArray
发送到图片datatype
列中的表格中):
private void FileSelectorButton_Click(object sender, EventArgs e)
{
OpenFileDialog ImageSelectorWindow = new OpenFileDialog();
ImageSelectorWindow.Filter = "JPG |*.jpg|PNG |*.png|Bitmap |*.bmp";
ImageSelectorWindow.Title = "Choose an Image";
if (ImageSelectorWindow.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = ImageSelectorWindow.FileName;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
picArray = new byte[fs.Length];
fs.Read(picArray, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
TBFilePath.Text = fileName;
}
以下是我的图片检索代码:
SqlCommand cmd2 = new SqlCommand("SELECT avatar FROM Members WHERE Id = '" + value + "'", con);
SqlDataReader dr = cmd2.ExecuteReader();
MemoryStream ms = new MemoryStream((byte[])dr[0]);
PBMemberImage.BackgroundImage = new Bitmap(ms);
这给了我以下例外:
有人可以帮我找出原因吗?
答案 0 :(得分:0)
Retrieve Images from sql server database
看看它并尝试下面的代码来检索您的图像。如果有效,请告诉我。
byte[] img = (byte[])cmd2.ExecuteScalar();
MemoryStream ms = new MemoryStream();
ms.Write(img, 0, img.Length);
PBMemberImage.BackgroundImage = new Bitmap(ms);