none
Convert byte to bitmap image error RRS feed

  • Question

  • While convert saved image in database to bitmap image error:

    While DR.Read()
               Dim pictureData As Byte() = CType(DR("Template"), Byte())
                Dim myimage As Image
                Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(pictureData)
                myimage = System.Drawing.Image.FromStream(ms) ''' HERE IS ERROR (Parameter is not valid.)
                DatabaseFingerprints.AsBitmap = myimage 
    
    End while

    error: Parameter is not valid at line: myimage = System.Drawing.Image.FromStream(ms)

    what is wrong?

    Thanks

    Thursday, May 23, 2019 1:18 AM

All replies

  • Hi,

    I tried your code,it works well.

    Imports System.Data.SqlClient
    Imports System.IO
    
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using conn = New SqlConnection(constr)
                conn.Open()
                cmd = New SqlCommand("select  image from Image where id=1", conn)
                Dim DR As SqlDataReader = cmd.ExecuteReader
                While DR.Read
                    Dim mybyte As Byte() = CType(DR("Image"), Byte())
                    Dim myimage As Image
                    Dim ms As MemoryStream = New MemoryStream(mybyte)
                    myimage = Image.FromStream(ms)
                    PictureBox1.Image = myimage
                End While
            End Using
        End Sub
    End Class
    

    I think the DR("Template")  is not an image format

    You can insert image into the database in the following way:

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using conn = New SqlConnection(constr)
                conn.Open()
                cmd = New SqlCommand("insert into Image (Id,Image) values (1,@blobdata)", conn)
                Dim picturePath As String = "D:\test.jpg"
                Dim fs As FileStream = New FileStream(picturePath, FileMode.Open, FileAccess.Read)
                Dim mybyte As Byte() = New Byte(fs.Length - 1) {}
                fs.Read(mybyte, 0, mybyte.Length)
                fs.Close()
                Dim prm As SqlParameter = New SqlParameter("@blobdata", SqlDbType.VarBinary, mybyte.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, mybyte)
                cmd.Parameters.Add(prm)
                cmd.ExecuteNonQuery()
            End Using
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 23, 2019 2:14 AM
  • Hello,

    I have a MSDN code sample which shows how to read images from a SQL-Server database table using similar code. Here is a stripped down version. Full source is here.

    Public Function GetImage(pIdentifier As Integer) As Image 
    
    	Dim fruitImage As Image = Nothing 
    
    	Using cn As New SqlConnection("Your connection string") 
    		Using cmd As New SqlCommand("SELECT Picture FROM dbo.Fruits WHERE id = @Id", cn) 
    
    			cmd.Parameters.AddWithValue("@id", pIdentifier) 
    
    			Dim reader As SqlDataReader 
    
    			cn.Open() 
    
    			reader = cmd.ExecuteReader(CommandBehavior.CloseConnection) 
    
    			If reader.Read Then 
    
    				Dim imageData = CType(reader(0), Byte()) 
    
    				reader.Close() 
    
    				If imageData IsNot Nothing Then 
    					Using ms As New MemoryStream(imageData, 0, imageData.Length) 
    						ms.Write(imageData, 0, imageData.Length) 
    						fruitImage = Image.FromStream(ms, True) 
    					End Using 
    				End If 
    
    			End If 
    
    
    		End Using 
    
    	End Using 
    
    	Return fruitImage 
    
    End Function 
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 23, 2019 2:23 AM
    Moderator
  • In order to check the data, try writing the image to disk, for example:

     

       Dim pictureData As Byte() = CType(DR("Template"), Byte())

       File.WriteAllBytes(“MyFile.png”, pictureData)

     

    Then verify the file. It should be non-empty. Check if you can view it using some graphical editor, such as Paint.

     

    Show some details about the code that writes these data to database.

    • Edited by Viorel_MVP Thursday, May 23, 2019 4:49 AM
    Thursday, May 23, 2019 4:46 AM
  • Thanks, 

    DR("Template")  is fingerprint template and SQL Server the field type is image.

    Insert template in database code"

    Dim da1 As New SqlDataAdapter("select * from xamed", MainSvr)
            da1.InsertCommand = New SqlCommand("INSERT INTO xamed (template,empno) Values(@template,@empno)", MainSvr)
            da1.InsertCommand.CommandType = CommandType.Text
            da1.InsertCommand.Parameters.Add("@template", SqlDbType.Binary, template.Size, "template")
            da1.InsertCommand.Parameters.Add("@empno", SqlDbType.Char).Value = FormEnroll.TextBox2.Text

    Is there any way retrieve template as bitmap image ?

    DatabaseFingerprints.AsBitmap = myimage 


    • Edited by kafsar Thursday, May 23, 2019 11:35 PM
    Thursday, May 23, 2019 11:25 PM
  • In order to check the data, try writing the image to disk, for example:

     

       Dim pictureData As Byte() = CType(DR("Template"), Byte())

       File.WriteAllBytes(“MyFile.png”, pictureData)

     

    Then verify the file. It should be non-empty. Check if you can view it using some graphical editor, such as Paint.

     

    Show some details about the code that writes these data to database.

    No, any image error: may file appeared to be damage, corrupted or its too large.

    DR("Template")  is fingerprint template and SQL Server the field type is image.

    Insert template in database code"

    Dim da1 As New SqlDataAdapter("select * from xamed", MainSvr)
            da1.InsertCommand = New SqlCommand("INSERT INTO xamed (template,empno) Values(@template,@empno)", MainSvr)
            da1.InsertCommand.CommandType = CommandType.Text
            da1.InsertCommand.Parameters.Add("@template", SqlDbType.Binary, template.Size, "template")
            da1.InsertCommand.Parameters.Add("@empno", SqlDbType.Char).Value = FormEnroll.TextBox2.Text

    Is there any way retrieve template as bitmap image ?

    DatabaseFingerprints.AsBitmap = myimage 

    Thursday, May 23, 2019 11:35 PM
  • My code returns an image, to create a Bitmap use the following constructor.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 24, 2019 12:00 AM
    Moderator
  • In order to check the data, try writing the image to disk, for example:

     

       Dim pictureData As Byte() = CType(DR("Template"), Byte())

       File.WriteAllBytes(“MyFile.png”, pictureData)

     

    Then verify the file. It should be non-empty. Check if you can view it using some graphical editor, such as Paint.

     

    Show some details about the code that writes these data to database.

    No, any image error: may file appeared to be damage, corrupted or its too large.

    DR("Template")  is fingerprint template and SQL Server the field type is image.

    Insert template in database code"

    Dim da1 As New SqlDataAdapter("select * from xamed", MainSvr)
            da1.InsertCommand = New SqlCommand("INSERT INTO xamed (template,empno) Values(@template,@empno)", MainSvr)
            da1.InsertCommand.CommandType = CommandType.Text
            da1.InsertCommand.Parameters.Add("@template", SqlDbType.Binary, template.Size, "template")
            da1.InsertCommand.Parameters.Add("@empno", SqlDbType.Char).Value = FormEnroll.TextBox2.Text

    Is there any way retrieve template as bitmap image ?

    DatabaseFingerprints.AsBitmap = myimage 

    Check other parts of your code and make sure that the template column actually receive a valid byte array. You use a specific form of SqlParameter that expects data in a DataTable in conjunction with data adapter.





    • Edited by Viorel_MVP Friday, May 24, 2019 4:51 AM
    Friday, May 24, 2019 4:34 AM