检索图像时参数无效

Parameter is not valid when i retrieve Image

string nama = dgGambar1.Rows[e.RowIndex].Cells["nama_gambar"].FormattedValue.ToString();
btnTambahDataBarang.Enabled = false;
koneksi.Open();
MySqlCommand command = new MySqlCommand("SELECT * FROM and_bwi_gambar WHERE nama_gambar = '" + nama + "'", koneksi);
MySqlDataReader rd;
rd = command.ExecuteReader();
while (rd.Read())
{
  txtGambar1.Text = rd.GetValue(2).ToString();
  byte[] img = (byte[])rd.GetValue(3);
  MemoryStream ms = new MemoryStream(img);
  pbGambar1.Image = Image.FromStream(ms);
}
koneksi.Close();

当我检索大小超过 250kb 的图像时,它总是出错 'parameter argument invalid'

可能有 2 个选项可以解决这个问题:

  1. 使用图像转换器:
    byte[] buffer = (byte[])rd.GetValue(3);
    using(var memStream = new MemoryStream(buffer))
    {
        var ic = new System.Drawing.ImageConverter();
        pbGambar1.Image = (System.Drawing.Image)ic.ConvertFrom(stream.ReadAllBytes());
    }
  1. 或尝试先将二进制数据加载到内存流中:
    byte[] buffer = (byte[])rd.GetValue(3);
    using(var memStream = new MemoryStream(buffer))
    {
        memStream.Position = 0;
        pbGambar1.Image = System.Drawing.Image.FromStream(memStream, false);
    }
}