【问题标题】:Convert Image from Db to Bitmap将图像从 Db 转换为位图
【发布时间】:2015-08-11 11:13:40
【问题描述】:

目前,我正在尝试从数据库中获取我的image,然后将其转换为Bitmap。我知道如何在VB 中做到这一点,但在C# 中,我真的不知道。

这是VB中的示例代码:

Dim tempimage As Bitmap = DataGridView1.Rows(i).Cells(0).Value
imagelist(i) = New Image(Of Gray, Byte)(tempimage)

上面的代码是我要转换成C#的代码。谢谢

【问题讨论】:

  • DataGridView1.Rows[i].Cells[0].Value 中有字节吗?
  • 我很抱歉,但我真的不明白

标签: c# vb.net bitmap


【解决方案1】:

如果你有byte[],那么试试这个来获取图片

pictureBox1.Image = GetImage((byte[])DataGridView1.Rows[i].Cells[0].Value);
public static Image GetImage(byte[] byteArrayIn)
{
    var ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

【讨论】:

    【解决方案2】:

    要将图像转换为位图,只需使用以下内容:

    Image YourImageFromGridView;
    Bitmap b = (Bitmap) YourImageFromGridView;
    

    也许这有帮助!

    【讨论】:

    • 如何进行数据网格视图??
    • 对不起,我没听懂你的意思。 @raaj5671
    • 没关系,我自己解决了
    【解决方案3】:

    相当于您的 VB 代码的 C# 代码是:

    Bitmap tempimage = (Bitmap)DataGridView1.Rows[i].Cells[0].Value;
    imagelist[i] = new Image<Gray, byte>(tempimage);
    

    我假设 DataGridView1 和 imagelist 在 VB 代码中声明如下(转换器需要知道这一点):

    Dim DataGridView1 As System.Windows.Forms.DataGridView
    Dim imagelist() As System.Windows.Forms.ImageList
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2012-10-24
      相关资源
      最近更新 更多