從數據庫中將圖像加載到picturebox中

[英]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無效”這樣的錯誤。

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

1 个解决方案

#1


1  

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

圖像已損壞。錯誤來自FromStream方法。您可以寫入磁盤並查看是否可以在圖像視圖中打開它。如果沒有,請檢查將其插入數據庫的代碼

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

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/31/917d7d3ad80805413303058ab6431712.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com