我能够成功地将图像存储在 SQL DB 中,图像有一个字段为 varchar50。这是我尝试过的代码
DataTable dt=new DataTable();
dt=neworder.Selectfromimage1();
if (dt.Rows.Count > 0)
{
// byte[] image =(byte[])dt.Rows[0]["image"];
byte image=Convert.ToByte(dt.Rows[0]["image"]);
MemoryStream stream = new MemoryStream(image);
//stream.Write(image, 0, image.Length);
stream.Seek(0,
SeekOrigin.Begin);
stream.Close();
btncompanion.Image =
Image.FromStream(stream);
}
我收到错误“输入字符串的格式不正确”
byte image=Convert.ToByte(dt.Rows[0]["image"]);
已编辑
保存图像的代码是
private byte[] ImageToStream(string fileName)
{
MemoryStream stream = new MemoryStream();
tryagain:
try
{
Bitmap image = new Bitmap(fileName);
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
// image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
}
catch (Exception )
{
goto tryagain;
}
return stream.ToArray();
}
fName ="C:\\Documents and Settings\\KAEM\\My Documents\\My Pictures\\images.jpg";
if (File.Exists(fName))
{
int id = 2;
byte[] content = ImageToStream(fName);
if (neworder.Insertintoimage1(content.ToString()))
{
}
}
else
{
MessageBox.Show(fName + " not found ");
}