Extract database image

1

I need to extract images from a database and then show them in unity and when I do the query I try to save the image in an array of bytes but it does not work. Also, the problem is to pass that array to a visible image.

<WebGet()>
<WebMethod()>
<OperationContract()>
Public Function prueba() As String
    Dim dt As New DataTable()
    dt.TableName = "Foto"
    dt.Columns.Add("foto")

    Dim s As String = "SELECT dbo.comedores2.mesa FROM dbo.comedores2"

    Dim SqlComman As String = s.ToString
    Dim ds As New DataSet()
    Using cmd As New SqlCommand(SqlComman, New SqlConnection(conexion))
        cmd.Connection.Open()
        Dim table As New DataTable()
        table.Load(cmd.ExecuteReader())
        ds.Tables.Add(table)
        cmd.Connection.Close()
    End Using

    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

        Dim byteArray As Byte() = ds.Tables(0).Rows(i).Item("mesa")
        dt.Rows.Add(byteArray)
    Next

    Using dt
        Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim rows As New List(Of Dictionary(Of String, Object))()
        Dim row As Dictionary(Of String, Object)
        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)()
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
        Return serializer.Serialize(rows)
    End Using

End Function
    
asked by joniggi 27.04.2017 в 12:43
source

1 answer

1

Unfortunately, I do not know the operations or requirements of Unity for the task you are doing. From what I've been able to review depends a lot on whether it's a sprite, if it's a texture, ect ...

However, I can still help you with the technical part of vb.net and tell you how you can format that array of bytes in an encoding that you can use.

1- Convert the image to StringBase64

When you see that you are using a JavascriptSerializer it is possible that you can pass the image in that format.

To convert the byte array to StringBase64 , we would do it in the following way:

Dim byteArray As Byte() = ds.Tables(0).Rows(i).Item("mesa")
Dim imageStringBase64 As String = Convert.ToBase64String(byteArray)
dt.Rows.Add(imageStringBase64)

2- Convert the byte [] into an image object

Another option that could be interesting for you is to create an image object:

Dim imagen as Image
Dim memoryStream as System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)
imagen = System.Drawing.Image.FromStream(memoryStream)

As I commented at the beginning, unfortunately my knowledge of Unity are purely conceptual and reviewing the code that you provide us I have not seen in which type of unity object you need to save the image to guess what the required format would be. However, the first option seems to me to have potential with what little I can know about it. Someone who knows Unity can surely tell you correctly, hopefully this answer can at least serve to try to point you the right way.

UPDATE 2017/04/27 16:27

I was reviewing and to associate a game object with an image you have to create an object of type Texture2D . The Texture2D objects have a LoadImage method that asks you to pass an image in StringBase64 , so the first option could be worth it. It would be something like this:

Dim byteArray As Byte() = ds.Tables(0).Rows(i).Item("mesa")
Dim imageStringBase64 As String = Convert.ToBase64String(byteArray)
Dim texture2D as Texture2D() = New Texture2D(1, 1)
texture2D.LoadImage(imageStringBase64)

We would be missing an additional step, which is to create a sprite starting from that texture that we just created (contribution of joniggi ):

Sprite sp2 = Sprite.Create(texture2D, new Rect(0, 0, ancho, alto), new Vector2(1.0f, 1.0f)); 
miPrefab.transform.GetComponent<Image>().sprite = sp2;
    
answered by 27.04.2017 / 15:45
source