保存从数据库检索到的图像时出错

时间:2018-08-30 07:49:11

标签: c# winforms picturebox

这是情况:

  1. 我可以保存()一个具有PixPictureBox图片的Employee条目 到数据库。
  2. 如果我编辑员工条目并从 PixPictureBox有一个新的,Update()很好。
  3. 这就是错误的出处,每当我编辑Employee条目并且不更改PixPictureBox的图像时,Update()方法都会引发以下异常:
  

$ exception {“ GDI +中发生一般错误。”} System.Runtime.InteropServices.ExternalException

此方法引发此错误:

private byte[] ImageToByteArray(Image imageIn)
{
    using (var ms = new MemoryStream())
    {
        imageIn.Save(ms, imageIn.RawFormat);
        return ms.ToArray();
    }
}

这是我的GetParams()方法,其中正在使用ImageToByteArray

private void GetParams()
{
    Ebm.ResetParams();
    Ebm.Params.Id = IdTextBox.Text.GetInt();
    Ebm.Params.EmployeeCode = Ebm.GenerateEmployeeCode();
    Ebm.Params.LastName = LastNameTextBox.Text;
    Ebm.Params.FirstName = FirstNameTextBox.Text;
    Ebm.Params.MiddleName = MiddleNameTextBox.Text;
    Ebm.Params.GenderId = GenderBox.SelectedValue.GetInt();
    Ebm.Params.BirthDate = BirthDatePicker.Value.GetDateTime();
    Ebm.Params.Age = AgeTextBox.Text.GetInt();
    Ebm.Params.Salary = SalaryTextBox.Text.GetDecimal();
    Ebm.Params.StatId = 1;

    if (PixPictureBox.Image != null)
    {
        Ebm.Params.Pics = ImageToByteArray(PixPictureBox.Image);
    }              
}

这是我用来将图片从数据库加载到PixPictureBox的方法

private Image ByteArrayToImage(byte[] bytesArr)
{
   using (var memstr = new MemoryStream(bytesArr))
   {
          Image img = Image.FromStream(memstr);
          return img;
   }
}

这是我的加载方式:

private void LoadDetails()
{
    IdTextBox.Text = Ebm.Item.Id.GetString();
    EmployeeCodeTextBox.Text = Ebm.Item.EmployeeCode;
    LastNameTextBox.Text = Ebm.Item.LastName;
    FirstNameTextBox.Text = Ebm.Item.FirstName;
    MiddleNameTextBox.Text = Ebm.Item.MiddleName;
    GenderBox.SelectedValue = Ebm.Item.GenderId;
    BirthDatePicker.Value = Ebm.Item.BirthDate;
    AgeTextBox.Text = Ebm.Item.Age.GetString();
    SalaryTextBox.Text = Ebm.Item.Salary.GetString();

    try
    {
        PixPictureBox.Image = ByteArrayToImage(Ebm.Item.Pics);
    }
    catch (Exception ex)
    {
        PixPictureBox.Image = null;
    }

}

我还从我的MemoryStream中得到了一个我认为与异常有关的错误:

enter image description here

1 个答案:

答案 0 :(得分:0)

我只需要将[13:26:34] -> POST 202 [message] hello [13:26:35] <- POST 200 Reply[message] Hello [13:26:35] <- POST 200 Reply[event] Debug Event [13:26:38] -> POST 202 [message] hi [13:26:39] <- POST 200 Reply[message] Hola [13:26:39] <- POST 200 Reply[event] Debug Event [13:26:46] -> POST 202 [message] status of Ankita [13:26:48] <- POST 200 Reply[message] Status of Ankita is RolledOn! [13:26:48] <- POST 200 Reply[event] Debug Event [13:56:20] -> POST 202 [conversationUpdate] [13:56:20] -> POST 202 [conversationUpdate] [13:56:31] -> POST 202 [message] Hi [13:56:32] <- POST 200 Reply[event] Debug Event [13:56:33] <- POST 200 Reply[message] Hola [13:56:33] <- POST 200 Reply[event] Debug Event 方法更改为此:

ByteArrayToImage()