本文介绍了将图像存储在SQL Server数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用vb和wcf Servics将图像存储在数据库varbinary列中 使用代码
I Am trying to store image in database varbinary Column using vb and wcf Servics Using code
Private Function SaveImage() As Byte() Dim arrayImage() As Byte = Nothing If (pic_meetPicture.Image Is Nothing) Then arrayImage = Nothing Else Try Dim ms As New MemoryStream Me.pic_meetPicture.Image.Save(ms, Me.pic_meetPicture.Image.RawFormat) arrayImage = ms.GetBuffer ms.Close() arrayImage = Process.Compressor.UICompressor.Compress(arrayImage) Catch ex As Exception End Try End If Return arrayImage End Function Public Shared Function Compress(ByVal buffer As Byte()) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim zip As GZipStream = New GZipStream(ms, CompressionMode.Compress, True) zip.Write(buffer, 0, buffer.Length) zip.Close() ms.Position = 0 Dim outStream As MemoryStream = New MemoryStream() Dim compressed() As Byte = New Byte(ms.Length) {} ms.Read(compressed, 0, compressed.Length) Dim gzBuffer() As Byte = New Byte(compressed.Length + 4) {} System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length) System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4) Return gzBuffer End Function用于显示图像
for display image
Private Sub SetImage(ByVal array As ArrayList) Dim arrayImage() As Byte = array(0).Image arrayImage = CType(Process.Compressor.UICompressor.Decompress(arrayImage), Byte()) If arrayImage IsNot Nothing Then Dim ms As New MemoryStream(arrayImage) pic_meetPicture.Image = Image.FromStream(ms) pic_meetPicture.SizeMode = PictureBoxSizeMode.StretchImage End If End SubPublic Shared Function Decompress(ByVal gzBuffer As Byte()) As Byte() Dim ms As MemoryStream = New MemoryStream() Dim msgLength As Integer = BitConverter.ToInt32(gzBuffer, 0) ms.Write(gzBuffer, 4, gzBuffer.Length - 4) Dim buffer() As Byte = New Byte(msgLength) {} ms.Position = 0 Dim zip As GZipStream = New GZipStream(ms, CompressionMode.Decompress) zip.Read(buffer, 0, buffer.Length) Return buffer End Function
但是当从数据库中获取图像时出现错误有效参数" 请帮助 感谢
But when fetch image from databse then getting error ''parameter in valid'' please Help thanks
推荐答案无需查看将数据存储在DB中的代码,我就可以确定问题出在哪里:停止将字符串连接起来以形成ans SQL命令! 请参阅此处:为什么我会得到一个参数无效."从数据库读取图像时出现异常? [ ^ ] Without looking at your code to store the data in teh DB, I can be pretty sure what the problem is: Stop concatenating strings to form ans SQL command! See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^]
更多推荐
将图像存储在SQL Server数据库中
发布评论