简体   繁体   English

从数据库将图片加载到图片框中

[英]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 '. 但是每次,我都会收到类似“ 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. 该错误来自FromStream方法。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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