Uploading an Image to Access


New member
Aug 29, 2004
Programming Experience
I am working on a program that needs to store images from a PictureBox into an Access database and later retreive (and display them) in the same PictureBox. I am stuck on the saving portion.

My "ideal" code would be something like the following:
objAddCommand.Parameters("Picture").Value = picImage.Image

Obviously, that doesn't work. "Picture" is an OLE Object (in Access). I read in other threads about storing the data into a FileStream ala:



... but I don't have a filename (I assume another button deals with the uploading of the picture into the PictureBox).

That said, I'm lost and stuck. Any help would be greatly appriciated!

Its not that I'm confused on the second thread, let me just cover the code part by part....
[color=#0000ff]Dim[/color][size=2] fs [/size][size=2][color=#0000ff]As[/color][/size][size=2] FileStream
[/size][size=2][color=#008000]'selecting the file to use int he filestream
[/color][/size][size=2]fs = [/size][size=2][color=#0000ff]New[/color][/size][size=2] FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
I don't want to use an OpenFileDialog here. I deal with loading the image elsewhere.

Alternately, what I'm trying to do to load the image into a stream by doing this:
[size=2][color=#0000ff]Dim[/color][/size][size=2] PICb [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Byte[/color][/size][size=2]() = [/size][size=2][color=#0000ff]New[/color][/size][size=2] [/size][size=2][color=#0000ff]Byte[/color][/size][size=2]() {}[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] ms [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.IO.MemoryStream = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.IO.MemoryStream[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] b [/size][size=2][color=#0000ff]As[/color][/size][size=2] Bitmap = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Bitmap(picImage.Image)[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] msLen [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = ms.Length[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] abyt() [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Byte
[/color][/size][size=2]b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
ms.Seek(0, IO.SeekOrigin.Begin)
ms.Read(abyt, 0, ms.Length)
objAddCommand.Parameters("Picture").Value = ms
... but this fails giving me:
"Buffer cannot be null." on line 905.
line 905	  [size=2]ms.Read(abyt, 0, ms.Length)[/size]

AAaaahhh! I'm pulling my hairs out ;)

The field picGCapa have the picture, i put the picture width "paste"

Dim newBitmap As Bitmap = New Bitmap(picGCapa.Image)
Dim _Imagem As Bitmap = New Bitmap(picGCapa.Image)
picGCapa.Tag = "C:\TestImage.bmp"
Dim fs As FileStream
Dim bw As BinaryReader
Dim OutByte() As Byte
fs = New FileStream(picGCapa.Tag, FileMode.Open, FileAccess.ReadWrite)
bw = New BinaryReader(fs)
OutByte = bw.ReadBytes(fs.Length)
Dim cmd As New OleDbCommand(SQLExecution.SQLStatement, sConn, _Trans)
Dim pms As OleDbParameterCollection = cmd.Parameters
pms.Add("@Imagem", OleDbType.VarBinary)
pms("@Imagem").Value = OutByte
pms = Nothing

Hi, i use thisd
Dim _Imagem As New clsImage
Dim bytBLOBData() As Byte = SQLExecution.SQLDataReader.Item("GCapa")
picGCapa.Image = _Imagem.myTratarImagem(bytBLOBData, txtGTitulo.Text)picGCapa.Show()
Public Function myTratarImagem(ByVal myfImagem() As Byte, ByVal myfNome As String, Optional ByVal myfIniciais As Boolean = True) As Bitmap
Dim bytBLOBData() As Byte = myfImagem
Dim bBLOBData() As Byte = bytBLOBData
Dim _n As Integer
Dim _Header As String
For _n = 0 To 512
if Not ChrW(bytBLOBData(_n).ToString) = Nothing Then
_Header = _Header & ChrW(bytBLOBData(_n).ToString)
End If
Dim _PosInicio As Integer = 0
If _Header.Substring(12, 35) = "Imagem de mapa de bitsPaint.Picture" Then_PosInicio = 88
ElseIf _Header.Substring(12, 25) = "Bitmap ImagePaint.Picture" Then
_PosInicio = 78
ElseIf _Header.Substring(12, 6) = "Imagem" Then
_PosInicio = 53
ElseIf _Header.Substring(12, 7) = "Picture" Then
_PosInicio = 53
End If
Dim strBLOBFilePath As String = myFichTempor & "\ImagemFoto.bmp"
Dim fsBLOBFile As New FileStream(strBLOBFilePath, FileMode.OpenOrCreate, FileAccess.Write)
fsBLOBFile.Write(bBLOBData, _PosInicio, bBLOBData.Length - _PosInicio)
_Stream = New Bitmap(strBLOBFilePath)
_bmp = CType(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
Return _bmp

the problem is "bold", i can´t read the image put in the database width
"paste", if you discovery please tell....

try it

There are a number of good examples on the internet and the one presented here will work. However, I have found it a lot easier to simply store a path name as to where on the disk the image can be found in a DB field rather than storing the image. First, the read is a lot faster. Two, the image is available outside the DB for viewing or editing. Three, there is a considerable jump in performance. Four, there is a lot less overhead in storing in a text field rather than a blob.

To use this concept you only have to read in the path name in a text variable and then set the image source to that.