arunvjadhav

i am trying to make one project in Vb to maint employee records with their photos

so i have made that table in database with the column for photo as image file but now after i run the program and load the image the image comes , alright but after a few seconds the program gives me a error that "external exception was unhandled" ; genericerror code in GDI+

please guide me how to get the solution for this.

the codes i have used is as follows....

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

Dim myStream As IO.Stream = Nothing

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.InitialDirectory = "c:\"

openFileDialog1.Filter = ".GIF files (*.gif)|*.gif"

openFileDialog1.FilterIndex = 2

openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Try

myStream = openFileDialog1.OpenFile()

If (myStream IsNot Nothing) Then

' Insert code to read the stream here.

PictureBox1.Image = Image.FromStream(myStream)

myStream.Close()

End If

Catch Ex As Exception

MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)

Finally

' Check this again, since we need to make sure we didn't throw an exception on open.

If (myStream IsNot Nothing) Then

myStream.Close()

End If

End Try

End If

End Sub

End Class

thanks




Re: Visual Basic Express Edition Load image in picturebox

DMan1

Why use the stream...use image.fromfile:

If OpenFIleDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Try

If IO.File.Exists(OpenFIleDialog1.FileName) Then

PictureBox1.Image = Image.FromFile(OpenFIleDialog1.FileName)

End If

Catch Ex As Exception

MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)

Finally

End Try

End If






Re: Visual Basic Express Edition Load image in picturebox

Ashish Basran

Do the error you talked about comes on some event after successful loading of image






Re: Visual Basic Express Edition Load image in picturebox

nobugz

The Paint event is probably throwing. If you use a Stream, you must keep it open after loading the image. GDI+ pages in bitmap data from the stream to keep memory pressure low. Avoid that mess and use DMan1's code.





Re: Visual Basic Express Edition Load image in picturebox

arunvjadhav

Thanks.. This help was very helpful. But the codes used by me also works. Actually i was loading a graphic interface file and not the GIF file, by mistake and so the error was being generated. Thanks anyway for the help. Your code also works great and is easy to understand.

I have another question if u can help. In the program which i am trying to make i have two textbox one for entering the Id number of employee and another for entering the name of employee. Now If somebody enters a name (string ) in the place of ID i.e number and if one enters the number in place of the name then i want the program to send a error message to the user to rectify the error. How do i do that . Thanks again for the time taken to answer my problem.

Arun





Re: Visual Basic Express Edition Load image in picturebox

arunvjadhav

Thanks for the help u have lend in here which is very useful for every body. But i am facing a problem with the same image load.

I am trying to create a Program with a database on the form in the Details view layout of the table called "Mak" in the Database Created by me Named as "store.mdf". This table has one coulmn for ID No , Name of Employee, Date of Join and Photo. The ID and Name are in the varchar(50), identity null , allownulls. And the photo has the property as image and the date of join has property as small date time.

Now when i load the data and photo in JPg format by using the add navigation control key, the data and the image loads correctly and i can view all the data and image correctly by using the navigation move next & previous controls. But when i press the save button on the Navigation control save item ; the program give a violation " Update requires a valid UpdateCommand when pressed DAtaRow collection with modified rows." at the line - Me.MakTableAdapter.Update(Me.storeDataset.Mak)

I tried to look in the help and i got to understand that i need to have a line as getHdc and release getHdc command to work around this problem as this database contains both Data+Graphics.

I dont know how to work around this problem. Kindly Help.