简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM