C# 在c中从sql检索图像#

C# 在c中从sql检索图像#,c#,sql,image,C#,Sql,Image,我能够成功地将映像存储在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

我能够成功地将映像存储在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);
 }
我收到错误“输入字符串的格式不正确” 在

已编辑

保存图像的代码是

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 ");
}

您必须弄清楚数据是如何编码以生成varchar字符串的。例如,如果它是base64,您将使用:


现在,由于您是存储图像的人,您应该知道图像是如何编码的,并且应该能够执行相反的过程将其取回。

您将图像的路径存储在数据库中。 您的代码应该如下所示:

string imagePath=dt.Rows[0]["image"].ToString();
            byte[] imageBytes;
            using (FileStream fs = File.Open(imagePath)) {
                 btncompanion.Image = Image.FromStream(fs);
            }

尝试此操作,未测试

在尝试读取之前,您正在关闭流。那不行。您应该尝试以下方法:

byte[] image=Convert.ToByte(dt.Rows[0]["image"]);
using (MemoryStream stream = new MemoryStream(image))
{
    btncompanion.Image = Image.FromStream(stream);
}
如果这不起作用,并给出一个GDI+错误,那么这是因为在反序列化映像之前关闭了流。在这种情况下,请使用:

byte image=Convert.ToByte(dt.Rows[0]["image"]);
MemoryStream stream = new MemoryStream(image);
btncompanion.Image = Image.FromStream(stream);
此外,在存储图像的代码中,可以从图像文件中获取字节,但将它们作为
string
传递到数据库。您知道图像字节可以包含从字符串中删除或终止字符串的不可打印字节吗

不要将图像存储为字符串,或者至少使用base64编码。其代码如下:

if (File.Exists(fName))
{
    byte[] content = ImageToStream(fName);
    string contentBase64 = Convert.ToBase64String(content);
    if (neworder.Insertintoimage1(contentBase64))
        ....
}
以及检索:

string image = dt.Rows[0]["image"];
byte[] imageBytes = Convert.FromBase64String(image);

MemoryStream stream = new MemoryStream(imageBytes);
btncompanion.Image = Image.FromStream(stream);

是否仅将文件系统路径的图像字节存储到图像?是的,仅存储路径,我还添加了保存代码请检查
goto
??你疯了吗?我添加代码也是为了保存,请看,如果有什么遗漏,如果你仔细看,他不会存储路径。但是他将图像字节存储为字符串,这是错误的。哪一个代码示例的最后一行?我的建议是根本不使用字符串来存储图像,而是使用
二进制
字段,这是C#中的
字节[]
。这样你就不必转换任何东西了。你的@Thorsten Dittmar检索字符串image=dt.Rows[0][“image”];byte[]imageBytes=Convert.FromBase64String(图像);MemoryStream stream=新的MemoryStream(imageBytes);btncompanion.Image=Image.FromStream(流);当然,你说的是我的代码示例——我想知道到底是哪一行。您是否在数据库中存储了64进制编码的字符串?
if (File.Exists(fName))
{
    byte[] content = ImageToStream(fName);
    string contentBase64 = Convert.ToBase64String(content);
    if (neworder.Insertintoimage1(contentBase64))
        ....
}
string image = dt.Rows[0]["image"];
byte[] imageBytes = Convert.FromBase64String(image);

MemoryStream stream = new MemoryStream(imageBytes);
btncompanion.Image = Image.FromStream(stream);