Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 8th, 2015, 06:40 PM
Friend of Wrox
Join Date: Jun 2003
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
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)

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

Dim RetVal AsLong
Dim FieldLen AsInt32
Dim AccessDataReader As OleDb.OleDbDataReader = AccessCommand.ExecuteReader()
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)
Any help would be greatly appreciated.
Old October 21st, 2015, 01:18 AM
Authorized User
Join Date: Jan 2010
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post

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.
Kushal Kaleshwari
Application Architect

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while loading image to picturebox owen_xgy VS.NET 2002/2003 0 March 28th, 2008 08:49 AM
Show/ Hide image while saving file. arnabghosh Javascript How-To 7 June 25th, 2007 11:49 PM
Name of image in PictureBox purefan Beginning VB 6 5 November 27th, 2006 02:20 PM
Saving/Retrieving Image file to Database davekrunal46 VB How-To 6 January 11th, 2006 04:41 AM
VB6 - PictureBox and Image controls Ole Jensen Beginning VB 6 3 July 14th, 2005 04:49 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.