Karen Wilson
asked on
How do I save an image to SQL that I have pasted into a picturebox via the clipboard?
I have a picturebox on my WinForm. The user inputs an image and it is saved into SQL.
If the saved image is a file and the user does this, the picture is saved and retrieved from SQL perfectly.
To input the image:
Dim openFileDialog As New OpenFileDialog
If openFileDialog.ShowDialog( ) = System.Windows.Forms.Dialo gResult.OK Then
PictureBox1.Image = System.Drawing.Image.FromF ile(openFi leDialog.F ileName)
End If
To save the image:
If PictureBox1.BackgroundImag e IsNot Nothing Then
Dim ms As New MemoryStream()
PictureBox1.BackgroundImag e.Save(ms, PictureBox1.BackgroundImag e.RawForma t)
Dim data As Byte() = ms.GetBuffer()
tblH.errImage = data
End If
To retrieve the image:
If tblH.errImage IsNot Nothing Then
Dim picImage = (tblH.errImage).ToArray()
If picImage.Length > 0 Then
Dim stream As MemoryStream
Dim img As System.Drawing.Image
stream = New MemoryStream(picImage)
img = System.Drawing.Image.FromS tream(stre am)
PictureBox1.Image = img
End If
Else
Me.PictureBox1.Image = Nothing
Me.PictureBox1.Refresh()
End If
But when I use the clipboard to insert the image, the system throws an error when I try to save it.
This Is the error:
An unhandled exception of type 'System.ArgumentNullExcept ion' occurred in System.Drawing.dll
Additional information: Value cannot be null.
I am using this code to insert an image from the clipboard:
If My.Computer.Clipboard.Cont ainsImage Then
PictureBox1.BackgroundImag e = My.Computer.Clipboard.GetI mage
PictureBox1.BackgroundImag eLayout = ImageLayout.Zoom
Else
MsgBox("Your clipboard does not contain an image.")
End If
If I change backgroundimage to image, it does the same thing. I also see there is a value in PictureBox1.BackgroundImag e.RawForma t so I don't understand the null value error.
I'm not sure where to check next so any direction will be greatly appreciated!
Many thanks!!
If the saved image is a file and the user does this, the picture is saved and retrieved from SQL perfectly.
To input the image:
Dim openFileDialog As New OpenFileDialog
If openFileDialog.ShowDialog(
PictureBox1.Image = System.Drawing.Image.FromF
End If
To save the image:
If PictureBox1.BackgroundImag
Dim ms As New MemoryStream()
PictureBox1.BackgroundImag
Dim data As Byte() = ms.GetBuffer()
tblH.errImage = data
End If
To retrieve the image:
If tblH.errImage IsNot Nothing Then
Dim picImage = (tblH.errImage).ToArray()
If picImage.Length > 0 Then
Dim stream As MemoryStream
Dim img As System.Drawing.Image
stream = New MemoryStream(picImage)
img = System.Drawing.Image.FromS
PictureBox1.Image = img
End If
Else
Me.PictureBox1.Image = Nothing
Me.PictureBox1.Refresh()
End If
But when I use the clipboard to insert the image, the system throws an error when I try to save it.
This Is the error:
An unhandled exception of type 'System.ArgumentNullExcept
Additional information: Value cannot be null.
I am using this code to insert an image from the clipboard:
If My.Computer.Clipboard.Cont
PictureBox1.BackgroundImag
PictureBox1.BackgroundImag
Else
MsgBox("Your clipboard does not contain an image.")
End If
If I change backgroundimage to image, it does the same thing. I also see there is a value in PictureBox1.BackgroundImag
I'm not sure where to check next so any direction will be greatly appreciated!
Many thanks!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER