且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数据库中保存图像

更新时间:2022-10-25 08:41:20

问题不在于保存到数据库,而在于您的图像。我建议阅读 c# - 发生一般性错误在GDI +中,JPEG图像到MemoryStream - 堆栈溢出 [ ^ ]因为我打赌你的图像来自你之前已经关闭的内存流。



马克

Am trying to insert an image into database table called Picture.

When I try to insert a (.png) image, I get no error but when I try to insert a (.jpeg) image or others apart from (.png) then I get this error
"A generic error occurred in GDI+".

This is my code

What I have tried:

btnsave ()

Try

Dim mstream as New System.IO.MemoryStream ()
PictureBox1.Image.Save(mstream, System. Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream. GetBuffer()

sql = " Insert into Picture (Pic) & _
"Values(@pic)"

Connect()
cmd = new OledbCommand (sql,conn)

cmd.parameters.Addwithvalue("pic", arrImage)
 cmd.ExecuteNonQuery()

Catch ex As Exception 
MsgBox (ex.Message)

End Try
End Sub

The problem isn't with saving to the database, it's with your image. I'd suggest reading up on c# - A generic error occurred in GDI+, JPEG Image to MemoryStream - Stack Overflow[^] as I bet your image is coming from a memory stream that you've already closed earlier.

Marc