Hello, I have a program that reads images from my access database. What I'm trying to do now is write the images out to a folder but I don't know where to begin. The "ms" variable holds the images.

Option Explicit On
Public Class Form1
    Private cn As ADODB.Connection
    Private rs As ADODB.Recordset
    Dim RecCount As Integer
    Dim test1
    Dim test2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cn = New ADODB.Connection
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                              "Data Source=C:\Users\Bao\Desktop\Capstone\db1.mdb"
        cn.Open()
        rs = New ADODB.Recordset
        rs.Open("Select * FROM student", cn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockBatchOptimistic, 1)
        'MsgBox("Total Number of records = " & rs.RecordCount)

        Do While Not rs.EOF
            'MsgBox(rs.Fields("firstname").Value)
            'MsgBox(rs.Fields("lastname").Value)
            Dim [B]ms [/B]As New IO.MemoryStream(CType(rs.Fields("image").Value, Byte()))

            'PictureBox1.Image.Save("C)
            rs.MoveNext()
        Loop

        cn.Close()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub
End Class

I tried to that but I keep getting an error....

At:
"picture = Image.FromStream(stream)"

It keeps saying "Parameter is not valid." Does anyone know why?

Option Explicit On
Imports System.IO
Imports System.Data.OleDb

Public Class Form1
    Private rs As ADODB.Recordset
    Dim test As OleDb.OleDbDataReader
    Dim RecCount As Integer
    Dim format As String = ".Jpeg"
    'Dim c As New cDibSection

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                              "Data Source=C:\Database3.accdb")
        cn.Open()
        Dim Command As New OleDbCommand("SELECT * FROM Table1", cn)
        Dim Reader As OleDbDataReader = Command.ExecuteReader()
        Dim picture As Image = Nothing

        While Reader.Read
            Dim pictureData As Byte() = CType(Reader.GetValue(2), Byte())

            Using stream As New IO.MemoryStream()
                picture = Image.FromStream(stream)
                stream.Write(pictureData, 78, pictureData.Length - 78)

            End Using

            My.Computer.Clipboard.SetImage(picture)
            If My.Computer.Clipboard.ContainsImage() Then
                picture = My.Computer.Clipboard.GetImage
                picture.Save("C:\Image\lalala.jpg")
            End If
        End While
        cn.Close()

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub
End Class

You have issues with this block of code:

While Reader.Read           
     Dim pictureData As Byte() = CType(Reader.GetValue(2), Byte())   
     Using stream As New IO.MemoryStream()               
        picture = Image.FromStream(stream)                
        stream.Write(pictureData, 78, pictureData.Length - 78)           
     End Using            
     My.Computer.Clipboard.SetImage(picture)    
     If My.Computer.Clipboard.ContainsImage() Then               
          picture = My.Computer.Clipboard.GetImage               
          picture.Save("C:\Image\lalala.jpg")          
     End If      
End While

Your trying to create an image from an empty stream. You need to at least write the bytes to the stream first. Or you can pass the bytes in the constructor.

Don't know what the clipboard situation is doing. I omitted it.

If your still getting parameter is invalid with this then its your byte() in question and you would need to include how your saving the array in the next post.

Dim MemoryStream As System.IO.MemoryStream

While Reader.Read
  MemoryStream = New System.IO.MemoryStream(CType(Reader.GetValue(2), Byte()))
  picture = Image.FromStream(MemoryStream)
  picture.Save("C:\Image\lalala.jpg")
  MemoryStream.Dispose()
  picture.Dispose()
End While

I still get Parameter is not valid at line:
picture = Image.FromStream(MemoryStream)

So I guess its my bytes? I don't understand what that means.

huft seems I want to do this but I dont know how to do this hahaha

Edited 5 Years Ago by ChronoSyntax: n/a

This article has been dead for over six months. Start a new discussion instead.