Kashif
Kashif

Reputation: 85

retrieve image from sql in c#

I am able to successfully store image in SQL DB, There is a field for image as varchar50. This is the code that I tried

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

I am getting error "Input string was not in corret format " on

byte image=Convert.ToByte(dt.Rows[0]["image"]);

EDITED

Code for saving image is

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

Upvotes: 0

Views: 4140

Answers (3)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

You are closing the stream before you're trying to read. That doesn't work. You should try the following:

byte[] image=Convert.ToByte(dt.Rows[0]["image"]);
using (MemoryStream stream = new MemoryStream(image))
{
    btncompanion.Image = Image.FromStream(stream);
}

If that doesn't work and gives a GDI+ error, then that is because the stream is closed before the image is deserialized. In that case use this:

byte image=Convert.ToByte(dt.Rows[0]["image"]);
MemoryStream stream = new MemoryStream(image);
btncompanion.Image = Image.FromStream(stream);

Also, in the code that stores the image, you get the bytes from the image file, but pass them as string to the database. You are aware of the fact that image bytes can contains non-printable bytes that are removed from a string or terminate the string?

Do not store the image as string or at least use base 64 encoding for that. The code for that would read:

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

And to retrieve:

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

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

Upvotes: 1

Adi
Adi

Reputation: 5223

You store the path to the image in your database. Your code should look like this:

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

Try this, not tested

Upvotes: 1

lc.
lc.

Reputation: 116538

You'll have to figure out how the data was encoded to produce the varchar string. For example, if it were base64, you would use Convert.FromBase64String:

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

Now, since you're the one storing the image, you should know how it is encoded and should be able to do the reverse process to get it back.

Upvotes: 0

Related Questions