Tuesday 6 March 2012

Data to Image

This little function converts image data field in database to System.Drawing.Image object
Consider a datatable with an image field named ImgItem, we'll call the function below like this :

Dim MyImg as System.Drawing.Image
MyImg = DataToImage(MyDS.tables("SomeTable").rows(0).Item("ImgItem")
   


Public Shared Function DataToImage(ByVal DataItem As Object) As System.Drawing.Image
        Dim arrImage() As Byte = DirectCast(DataItem, Byte())
        Dim ms1 As New System.IO.MemoryStream(arrImage)
        Dim origimage As System.Drawing.Image = System.Drawing.Image.FromStream(ms1)
        Return origimage
End Function

When It comes to saving

                Dim ms As New System.IO.MemoryStream
                Me.ImgFoto.Image.Save(ms, ImgFoto.Image.RawFormat)
                Dim arrImage() As Byte = ms.GetBuffer
                ms.Close()


Now you may use arrImage to save into a datafield.

This is my way of saving. I'll explain this way later.

                MyPair.Add("Fotograf", arrImage)
        

No comments:

Post a Comment