sanu j
sanu j

Reputation: 85

Loading image in picturebox from database

i've tried the following code for loading an image in picturebox from database. but everytime , i get an error like 'paramater is not valid'.

buttonSave()
            {
                .......
                .......
                img = Image.FromFile(strFileName);
                byte[] byteImg = ImageToByteArray(img);
                objEmp.Picture = byteImg;
                .......
                .......
            } 

public byte[] ImageToByteArray(Image img)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }  

Display()
       { 
           .......
           .......
           Byte[] bytePicData = (Byte[])dt.Rows[0]["PICTURE"];                
           MemoryStream stmPicData = new MemoryStream(bytePicData);
           PicBox.Image = Bitmap.FromStream(stmPicData);} 
           .......
           .......
      }

Upvotes: 0

Views: 1359

Answers (1)

AbdElRaheim
AbdElRaheim

Reputation: 1394

the image is corrupt. The error is from the FromStream method. Can you write to disk and see if you can open it in an image view. If not then check the code where you are inserting it into the database

Byte[] bytePicData = (Byte[])dt.Rows[0]["PICTURE"];
// Save
File.WriteAllBytes("out.bmp", bytePicData);

MemoryStream stmPicData = new MemoryStream(bytePicData);
PicBox.Image = BitMap.FromStream(stmPicData);

Upvotes: 1

Related Questions