我收到错误:当我从oledb数据库中检索图像时参数无效

时间:2017-08-01 04:15:51

标签: c# image winforms fetch

当我调用此方法时,它显示错误:无效参数请帮助

    public void DisplayDoc()
    {
        try
        {
            con = new OleDbConnection(constr);
            con.Open();

            OleDbCommand cmd = new OleDbCommand();
            cmd = new OleDbCommand("Select pic from doc where adharno= '" + this.aadhar + "'", con);

            OleDbDataReader dr = cmd.ExecuteReader();

            DataTable dt = new DataTable(); 
            dt.Load(dr);

            DataRow row = dt.Rows[0];

            pictureEmployeePhoto.Image = LoadPhoto((byte[])row["pic"]);

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error :" + ex.Message);
        }
    }

    private Image LoadPhoto(byte[] photo)
    {
        MemoryStream ms = new MemoryStream(photo);
        return Image.FromStream(ms); //Getting Error Here: Invalid Parameter
    }

here is my Doc Table in which i have stored image as shown

以下用于在数据库中存储图像的方法

    byte[] Pic;
    OpenFileDialog ofdPhoto;

    MemoryStream ms = new MemoryStream();

    private void btnPhotoUpload_Click(object sender, EventArgs e)
    {
        // open file dialog 
        ofdPhoto = new OpenFileDialog();
        // image filters
        ofdPhoto.Title = "Select Employee Photo";
        ofdPhoto.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
        ofdPhoto.FileName = null;

        if (ofdPhoto.ShowDialog() == DialogResult.OK)
        {
            // display image in picture box
            pictureEmployeePhoto.Image = new Bitmap(ofdPhoto.FileName);
            // image file path
            txtPhotoFilePath.Text = ofdPhoto.FileName;

            try
            {
                // Here get_image is a function and Big is the byte[] type
                Pic = get_image();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message.ToString());
            }
        }
    }

    public byte[] get_image()
    {
        MemoryStream ms = new MemoryStream();
   pictureEmployeePhoto.Image.Save(ms,pictureEmployeePhoto.Image.RawFormat);
        return ms.GetBuffer();
    }

1 个答案:

答案 0 :(得分:0)

试试这段代码。

public Image byteArrayToImage(byte[] byteArrayIn)
{
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    Image img = (Image)converter.ConvertFrom(byteArrayIn);
    return img;
}