Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Am trying to store image in database varbinary Column using vb.net and wcf Servics

Using code
VB
Private Function SaveImage() As Byte()
        Dim arrayImage() As Byte = Nothing
        If (pic_meetPicture.Image Is Nothing) Then
            arrayImage = Nothing
        Else
            Try
                Dim ms As New MemoryStream
                Me.pic_meetPicture.Image.Save(ms, Me.pic_meetPicture.Image.RawFormat)
                arrayImage = ms.GetBuffer
                ms.Close()
                arrayImage = Process.Compressor.UICompressor.Compress(arrayImage)
            Catch ex As Exception
            End Try
        End If
        Return arrayImage
    End Function

Public Shared Function Compress(ByVal buffer As Byte()) As Byte()

            Dim ms As MemoryStream = New MemoryStream()
            Dim zip As GZipStream = New GZipStream(ms, CompressionMode.Compress, True)
            zip.Write(buffer, 0, buffer.Length)
            zip.Close()
            ms.Position = 0

            Dim outStream As MemoryStream = New MemoryStream()
            Dim compressed() As Byte = New Byte(ms.Length) {}
            ms.Read(compressed, 0, compressed.Length)

            Dim gzBuffer() As Byte = New Byte(compressed.Length + 4) {}
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length)
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4)
            Return gzBuffer
        End Function

for display image
VB
Private Sub SetImage(ByVal array As ArrayList)
        Dim arrayImage() As Byte = array(0).Image
        arrayImage = CType(Process.Compressor.UICompressor.Decompress(arrayImage), Byte())
        If arrayImage IsNot Nothing Then
            Dim ms As New MemoryStream(arrayImage)
            pic_meetPicture.Image = Image.FromStream(ms)
            pic_meetPicture.SizeMode = PictureBoxSizeMode.StretchImage
        End If
    End Sub

VB
Public Shared Function Decompress(ByVal gzBuffer As Byte()) As Byte()
            Dim ms As MemoryStream = New MemoryStream()
            Dim msgLength As Integer = BitConverter.ToInt32(gzBuffer, 0)
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4)
            Dim buffer() As Byte = New Byte(msgLength) {}
            ms.Position = 0

            Dim zip As GZipStream = New GZipStream(ms, CompressionMode.Decompress)
            zip.Read(buffer, 0, buffer.Length)

            Return buffer

        End Function


But when fetch image from databse then getting error 'parameter in valid'
please Help
thanks
Posted
Updated 16-Dec-14 22:34pm
v3
Comments
Tomas Takac 17-Dec-14 4:30am    
This is all irrelevant. You should show the code where you retrive the image form the database.
Naim_86 17-Dec-14 4:33am    
I am Using WCF Service to get data from database and Store in Array list

DB Column in Varbinary(max) to store and retriew image

1 solution

Without looking at your code to store the data in teh DB, I can be pretty sure what the problem is: Stop concatenating strings to form ans SQL command!

See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900