none
How do I assign a graphics image to an EF entity field and vice versa? RRS feed

  • Question

  • WHAT I HAVE:

    VB 2010, .NET 4.0, EF 4.0, SQL Express

    MY PROBLEM:

    Can you give me a VB 2010 code example of how one would, say, assign the image in a (Windows Forms) picture box to an EF entity field (or vice versa)? (BTW, I'm using Model-First.)


    Wednesday, February 19, 2014 2:46 AM

Answers

  • The above post says that in order to transfer an entity field for a picture (a Byte array) to an Image object, one needs to first transfer it to a picture file. What about using a memory stream, like what was proposed for the other way around (Image-to-entity)? Using a file requires getting a unique temporary file name, using the file to make the transfer, and deleting that file after the fact--and it's slower--so if there's a relatively simple way to avoid using an actual file, I'd like to know.

    Heck, we don't even do that in Web UI(s). You have a pathing to a physical picture file that gets loaded. :)

    Wednesday, August 5, 2015 8:54 PM

All replies

  • Hello,

    >>Can you give me a VB 2010 code example of how one would, say, assign the image in a (Windows Forms) picture box to an EF entity field (or vice versa)? (BTW, I'm using Model-First.)

    In SQLServer, we store the image or photos as BINARY type. So first we need to change the picture to a byte stream and save it to database.

    Imports System.IO
    
    
    Module Module1
    
    
        Sub Main()
    
    
            'Create The Byte Stream
    
    
            Dim filePath As String = "E:\19-1.JPG"
    
    
            Dim fileStream As New FileStream(filePath, FileMode.Open)
    
    
            Dim filesize As Long = fileStream.Length
    
    
            Dim buffer(filesize) As Byte
    
    
            fileStream.Read(buffer, 0, filesize)
    
    
            fileStream.Close()
    
    
            'Store Image Into DataBase
    
            Dim db As DataBaseFirstDBEntities = New DataBaseFirstDBEntities()
    
    
            Dim ImageTable As ImageTable = New ImageTable()
    
    
            ImageTable.Id = 1
    
    
            ImageTable.Picture = buffer
    
    
            db.ImageTables.Add(ImageTable)
    
    
            db.SaveChanges()
    
    
            'Read Image From DB
    
            Dim NewPicutre As String = "E:\19-2.JPG"
    
    
            Dim imageTableNew As ImageTable = db.ImageTables.FirstOrDefault
    
    
            Dim NewfileStream As New FileStream(newPicutre, FileMode.Create)
    
    
            NewfileStream.Write(imageTableNew.Picture, 0, imageTableNew.Picture.Length)
    
    
            NewfileStream.Close()
    
    
        End Sub
    
    
    End Module
    

    The Result:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 20, 2014 2:30 AM
    Moderator
  • This seems to explain how to transfer a picture back and forth between an entity and a FILE. What about between an entity and a control?

    Robert Gustafson

    Thursday, February 20, 2014 3:51 AM
  • Save Image to a MemoryStream and get the bytes from there:

    Dim ms As New IO.MemoryStream
    
            PictureBox1.Image.Save(ms, Imaging.ImageFormat.Jpeg)
    
            Dim bytes() As Byte = ms.GetBuffer()
    
            ms.Close()
    
            'Store Image Into DataBase
    
            Dim db As DataBaseFirstDBEntities = New DataBaseFirstDBEntities()
    
            Dim ImageTable As ImageTable = New ImageTable()
    
            ImageTable.Id = 1
    
            ImageTable.Picture = bytes
    
            db.ImageTables.Add(ImageTable)
    
            db.SaveChanges()
    

    To read, it needs us firstly to save the data to a picture and then read it:

       Dim NewPicutre As String = "E:\19-2.JPG"
    
    
            Dim db As DataBaseFirstDBEntities = New DataBaseFirstDBEntities()
    
    
            Dim imageTableNew As ImageTable = db.ImageTables.FirstOrDefault
    
    
            Dim NewfileStream As New FileStream(NewPicutre, FileMode.Create)
    
    
            NewfileStream.Write(imageTableNew.Picture, 0, imageTableNew.Picture.Length)
    
    
            NewfileStream.Close()
    
    
            PictureBox2.Image = Image.FromFile(NewPicutre)
    

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 20, 2014 9:02 AM
    Moderator
  • The above post says that in order to transfer an entity field for a picture (a Byte array) to an Image object, one needs to first transfer it to a picture file. What about using a memory stream, like what was proposed for the other way around (Image-to-entity)? Using a file requires getting a unique temporary file name, using the file to make the transfer, and deleting that file after the fact--and it's slower--so if there's a relatively simple way to avoid using an actual file, I'd like to know.


    Robert Gustafson




    Wednesday, August 5, 2015 8:08 PM
  • The above post says that in order to transfer an entity field for a picture (a Byte array) to an Image object, one needs to first transfer it to a picture file. What about using a memory stream, like what was proposed for the other way around (Image-to-entity)? Using a file requires getting a unique temporary file name, using the file to make the transfer, and deleting that file after the fact--and it's slower--so if there's a relatively simple way to avoid using an actual file, I'd like to know.

    Heck, we don't even do that in Web UI(s). You have a pathing to a physical picture file that gets loaded. :)

    Wednesday, August 5, 2015 8:54 PM