image.save stream 到 image.fromstream 问题
我的目的是把图片保存到SQL数据库中,我用stream 操作,写入是正常的,读出就不正常了,
为了查原因,我把代码减化后,发现是我用MemoryStream 出错,但不知道怎么去改.
我用过 image.save(path)到 filestream是正常的,但是,我不想中间多个写磁盘文件的过程,
Dim ms As New IO.MemoryStream
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim d() As Byte
ReDim d(ms.Length)
ms.Read(d, 0, ms.Length - 1)
Dim obj As Object
obj = d
d = obj
ms = New IO.MemoryStream(d)
PictureBox2.Image = Image.FromStream(ms) '运行到此句出错无效 ,"参数无效"
[解决办法]
我了解了你的目的,请看下面的代码,我这里调试通过
'界面上有两个PictureBox,名字分别为picture1和picture2'下面的代码是从picture1的image复制到picture2中Imports System.IO'......Dim stm1 As New MemoryStreampicture1.Image.Save(stm1, System.Drawing.Imaging.ImageFormat.Bmp)Dim imageByteData As Byte() = stm1.ToArrayDim stm2 As New MemoryStream(imageByteData)picture2.Image = Image.FromStream(stm2)stm1.Dispose()stm2.Dispose()
[解决办法]
Dim ms As New IO.MemoryStream PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) Dim d() As Byte '将下面两句代码改为 'ReDim d(ms.Length) 'ms.Read(d, 0, ms.Length - 1) '这句就OK了,因为ms.Length是流的长度,ms.GetBuffer()得到的是总容量的 d = ms.GetBuffer() Dim obj As Object obj = d d = obj ms = New IO.MemoryStream(d) PictureBox2.Image = Image.FromStream(ms) '运行到此句出错无效 ,"参数无效"