如何从Mysql中将图像显示到Devexpress GridView / Devexpress卡视图中

时间:2015-09-29 08:04:45

标签: mysql vb.net devexpress

这里我想问一下,如何将图像数据显示到DevExpress DevExpress gridview或CardView中。 因为我将从mysql数据库中获取数据,并保留BLOB数据类型。

如果使用visual basic如何或其代码。?

我使用的代码:

Dim Query As String = "SELECT FTO.ALM_NIM as FOTO_ID, FTO.ALM_FOTO as FOTO FROM tbl_foto_alumni as FTO"

Using DA As New MySql.Data.MySqlClient.MySqlDataAdapter(Query, MyKonection)
   Dim DTwisda As New DataTable
   DA.Fill(DTwisda)
   if Dtwisda.Rows.Count > 0 Then
      GridControl.Datasource = DTwisda
      MainView = CardView1
      Dim RepItemImg As New Repository.RepositoryItemPictureEdit()
      RepItemImg.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch
      RepItemImg.BestFitWidth = 100
      RepItemImg.CustomHeight = 150
      With CardView1.Columns("FOTO")
           .ColumnEdit = RepItemImg
      End With
      With CardView1
           .OptionsBehavior.FieldAutoHeight = True
           .MaximumCardRows = 2
           .MaximumCardColumns = 6
      End With
   End If
End Using

要Screnshoot我的程序和从数据库调用数据的结果,请检查附件。 我已附上以下单词文件。

ScreenShoot_Program

请帮助并提供解决方案。

此致

Tafary

1 个答案:

答案 0 :(得分:0)

试用此代码:

Dim row As DataRow
            Dim i As Integer
            For Each row In myDt.Rows
                Column = New DataColumn
                With Column
                    .DataType = GetType(Bitmap)
                    ' .DefaultValue = Base64ToImage(row("Photo"))
                    .Unique = False
                    .ColumnName = "EmpPhoto"
                End With
                If Not myDt.Columns.Contains("EmpPhoto") Then
                    myDt.Columns.Add(Column)
                End If
                myDt.Rows(i)("EmpPhoto") = Base64ToImage(row("Photo"))
                i += 1
                myDt.AcceptChanges()
            Next
            i = 0
            'myDt.Rows.Add(Column.DefaultValue)

 Public Function Base64ToImage(base64String As String) As Image
    Try
        ' Convert Base64 String to byte[]
        Dim imageBytes As Byte() = Convert.FromBase64String(base64String)
        Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
        ' Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length)
        Dim image1 As Image = Image.FromStream(ms, True)
        Return image1
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Function