View Single Post
  #1 (permalink)  
Old October 8th, 2015, 06:40 PM
jmss66 jmss66 is offline
Friend of Wrox
Points: 1,035, Level: 12
Points: 1,035, Level: 12 Points: 1,035, Level: 12 Points: 1,035, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to jmss66
Default Saving an image to a file from a PictureBox

I am reading a table off an Access database and displaying an image from one of its field to a picture box. I am able to display the image but when I try to save it to a file in a jpeg format, I am getting an error when it hits the line where the code saves the image to a file. I tried a couple of ways to assign a filename but I still get the error message below.

" A generic error occurred in GDI+"

Here is my code below
Code:
 
PrivateSub btnReader_Click(sender AsObject, e AsEventArgs) Handles btnReader.Click
Dim AccessConnection As OleDb.OleDbConnection
'Dim drTemplate As DataRow
Dim tblTemplate AsNewDataTable
Dim daTemplate AsNew OleDb.OleDbDataAdapter
Dim AccessCommand AsNew OleDb.OleDbCommand
Dim builder AsNew OleDb.OleDbCommandBuilder
Dim sqlString AsString
Dim strPathFileName AsString
AccessConnection = New OleDb.OleDbConnection(MDBConnString)
AccessConnection.Open()

sqlString = "SELECT BLOB_1, IS_KEY, Text_1_25, Text_2_25 FROM ID_PRO WHERE IS_Key = '1'"
AccessCommand = New OleDb.OleDbCommand(sqlString, AccessConnection)
daTemplate = New OleDb.OleDbDataAdapter(AccessCommand)
builder = New OleDb.OleDbCommandBuilder(daTemplate)
daTemplate.MissingSchemaAction = MissingSchemaAction.AddWithKey
tblTemplate = NewDataTable
daTemplate.Fill(tblTemplate)

Dim RetVal AsLong
Dim FieldLen AsInt32
 
Dim AccessDataReader As OleDb.OleDbDataReader = AccessCommand.ExecuteReader()
AccessDataReader.Read()
FieldLen = AccessDataReader.Item(0).Length
Dim PictureByteArray(FieldLen - 1) AsByte
Dim startIndex AsInteger = 0
RetVal = AccessDataReader.GetBytes(0, startIndex, PictureByteArray, 0, PictureByteArray.Length)
Dim BLOBDataStream AsNew IO.MemoryStream(PictureByteArray)
Me.PictureBox1.Image = Image.FromStream(BLOBDataStream)
strPathFileName = "C:\OLEtoJPEG\Pictures\" & tblTemplate.Rows(0).Item("Text_1_25") & " " & tblTemplate.Rows(0).Item("Text_2_25") & ".jpg"
Me.PictureBox1.Image.Save("C:\OLEtoJPEG\Pictures\" & tblTemplate.Rows(0).Item("Text_2_25") & " " & tblTemplate.Rows(0).Item("Text_1_25") & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.PictureBox1.Image.Save("C:\Test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
 
AccessDataReader.Close()
AccessConnection.Close()
EndSub
Any help would be greatly appreciated.
Thanks
Reply With Quote