当我调用此方法时,它显示错误:无效参数请帮助
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
}
以下用于在数据库中存储图像的方法
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();
}
答案 0 :(得分:0)
试试这段代码。
public Image byteArrayToImage(byte[] byteArrayIn)
{
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
Image img = (Image)converter.ConvertFrom(byteArrayIn);
return img;
}