p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   VB.NET (http://p2p.wrox.com/vb-net-83/)
-   -   Saving an image to a file from a PictureBox (http://p2p.wrox.com/vb-net/95095-saving-image-file-picturebox.html)

jmss66 October 8th, 2015 07:40 PM

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

kushalkg October 21st, 2015 02:18 AM

make sure that the path exists and the logged in user account has write access to it. Easiest way is to try to create a test file in the destination folder from windows explorer.


All times are GMT -4. The time now is 09:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.