这段使用MemoryStream的代码为什么会导致“无效参数”?!!!!!!

cyberfoxoooo 2004-04-30 10:10:43
前段时间问了一个关于用ADO.NET如何从SQLserver中显示image数据类型的问题,得到了一段代码,如下:

Dim con As New SqlConnection("Server=hz-lfy;uid=sa;pwd=;database=MyTestDb")
Dim da As New SqlDataAdapter("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet

con.Open()
da.Fill(ds, "MyImages")

Dim myRow As DataRow
myRow = ds.Tables("MyImages").Rows(0)

Dim MyData() As Byte
MyData = myRow("imgField")
Dim K As Long
K = UBound(MyData)

Dim w As New MemoryStream(MyData)

'关键在这里,Me.PictureBox1.Image = System.Drawing.Image.FromStream(w)捕捉到异常“参数错误“,查了一些资料也没有解决这个问题,甚至在运行《ADO.NET技术内幕》这本书第十三章的一个示例代码中也产生了这个'错误,后面再详述。

Try
Me.PictureBox1.Image = System.Drawing.Image.FromStream(w)

Catch ex As Exception
MsgBox(ex.Message)
End Try

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing


在微软.net程序员系列丛书的《ADO.NET技术内幕》这本书的第十三章有一个ShowEmployeesPhotos示例,用来从Northwind数据库中的Employee表中读取数据,这个表中有一个Photo列存储着IMAGE类型的字段,我运行这个示例的时候仍然存在着“无效参数”问题。具体的代码位置是在:
Dim strm As New IO.MemoryStream(aChildRows(0).Photo)
Dim bmp As New Bitmap(strm)
picPhoto.Image = bmp

到了这行语句Dim bmp As New Bitmap(strm)就产生了“无效参数”提示。

各位大虾帮忙看看到底是什么问题。
...全文
179 9 点赞 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cyberfoxoooo 2004-05-03
谢谢~!!
  • 打赏
  • 举报
回复
wolftop 2004-05-03
UP~!UP!~
  • 打赏
  • 举报
回复
LiJoe 2004-05-02
Categories中存储的image是在bmp图片前面加多了78个字节,
如果你想读出Categories表中的原记录并显示,可以用下面办法将
前面78字节去掉后显示

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection("Server=HH-LFY;uid=sa;pwd=;database=Northwind")
Dim da As New SqlDataAdapter("Select * From Categories ", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet

con.Open()
da.Fill(ds, "Categories")

Dim myRow As DataRow
myRow = ds.Tables("Categories").Rows(0)

Dim MyData() As Byte
MyData = myRow("Picture")
Dim K As Long
K = UBound(MyData)

Dim MyData2(K - 78) As Byte

Dim i As Integer
For i = 0 To K - 78
MyData2(i) = MyData(i + 78)
Next

Dim w As New MemoryStream(MyData2)

Try
Me.PictureBox1.Image = System.Drawing.Image.FromStream(w)
Catch ex As Exception
MsgBox(ex.Message)
End Try

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing

End Sub
  • 打赏
  • 举报
回复
LiJoe 2004-05-02
不同的写入方法可能造成写入mssql数据库中图片的格式发生变化,
你用下面的代码将图片写入

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim con As New SqlConnection("Server=HH-LFY;uid=sa;pwd=;database=Northwind")
Dim da As New SqlDataAdapter("Select * From Categories", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet

con.Open()
da.Fill(ds, "Categories")

Dim myRow As DataRow
myRow = ds.Tables("Categories").NewRow
myRow(1) = "test1"
myRow(2) = "test2"

Dim fs As New FileStream("c:\1.bmp", FileMode.Open)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()

myRow(3) = MyData

ds.Tables("Categories").Rows.Add(myRow)

da.Update(ds, "Categories")

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing
End Sub


用下面的读出
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection("Server=HH-LFY;uid=sa;pwd=;database=Northwind")
Dim da As New SqlDataAdapter("Select * From Categories where CategoryID= 9", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet

con.Open()
da.Fill(ds, "Categories")

Dim myRow As DataRow
myRow = ds.Tables("Categories").Rows(0)

Dim MyData() As Byte
MyData = myRow("Picture")
Dim K As Long
K = UBound(MyData)

Dim w As New MemoryStream(MyData)
Try
Me.PictureBox1.Image = System.Drawing.Image.FromStream(w)
Catch ex As Exception
MsgBox(ex.Message)
End Try

MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing

End Sub


  • 打赏
  • 举报
回复
pisces007 2004-05-02
Dim arr() As Byte = CType(你的dataset.Tables(0).Rows(你要显的行)("Picture"), Byte())
Dim ms As New MemoryStream(arr)
With PictureBox1
.Image = Image.FromStream(ms)
.SizeMode = PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
End With
  • 打赏
  • 举报
回复
LiJoe 2004-05-01
这个属于图片格式不正确
  • 打赏
  • 举报
回复
cyberfoxoooo 2004-05-01
。。。这个问题真的这么难么,!!!
  • 打赏
  • 举报
回复
cyberfoxoooo 2004-04-30
是说 aChildRows(0).Photo 和 myRow = ds.Tables("MyImages").Rows(0) 是否有效么?
没问题的,都是有效的。
我不用MemoryStream,而是用FileStream做了同样的试验,还是不行!!!!

下面是在Northwind数据库中,Categories表中取得picture字段存为文件并显示,可是在语句PictureBox1.Image = New Bitmap("c:\5.bmp")
中,出现了同样的问题“使用了无效参数”。

Dim data As Byte() = DataSet1.Tables("Categories").Rows(7).Item(0)
Dim myfilestream As New System.IO.FileStream("c:\5.bmp", IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
PictureBox1.Image = New Bitmap("c:\5.bmp")

在C盘根目录下产生了一个大小为10.4KB的5.bmp文件,可见的确从数据库中读出了picture字段,可是还是提示“无效参数”,而且在WINDOWS下直接打开什么也看不到~!!





  • 打赏
  • 举报
回复
存储那个文件的路径对不对啊???

你仔细的核对一下,他的路径绝对是错的
  • 打赏
  • 举报
回复
相关推荐
发帖
VB
加入

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2004-04-30 10:10
社区公告
暂无公告