Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I save an image to SQL that I have pasted into a picturebox via the clipboard?

Posted on 2014-10-06
2
Medium Priority
?
884 Views
Last Modified: 2014-10-13
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.DialogResult.OK Then
            PictureBox1.Image = System.Drawing.Image.FromFile(openFileDialog.FileName)
End If

To save the image:
If PictureBox1.BackgroundImage IsNot Nothing Then
                Dim ms As New MemoryStream()
                PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
                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.FromStream(stream)
                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.ArgumentNullException' 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.ContainsImage Then
            PictureBox1.BackgroundImage = My.Computer.Clipboard.GetImage
            PictureBox1.BackgroundImageLayout = 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.BackgroundImage.RawFormat 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!!
0
Comment
Question by:Karen Wilson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
ktaczala earned 2000 total points
ID: 40375694
This line is your problem:
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)

Try This:
PictureBox1.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) ' or whatever format you want.
0
 

Author Closing Comment

by:Karen Wilson
ID: 40377311
Worked perfect!!  Thanks so much.
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question