[VB.NET]关于保存图片问题

前端之家收集整理的这篇文章主要介绍了[VB.NET]关于保存图片问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

关于保存图片问题
我想把图片显示在PictureBox里的图片
保存到数据库中去.然后再拿出来
请问有什么办法.谁能给我一个例子
谢谢
__________________________________________________________________________
http://blog.csdn.net/Montaque/archive/2002/09/06/10531.aspx
把bmp改为PictureBox.Image即可
__________________________________________________________________________
添加产品图片
Private Sub Lg_AddProductPic()
Dim OpenFile As New OpenFileDialog
With OpenFile
.FileName =
.Filter = bmp files(*.bmp)|*.bmp|gif files(*.gif)|*gif|All files(*.*)|*.*
.ShowDialog()
If .FileName = Then Exit Sub
If Dir(.FileName) = Then Exit Sub
Me.PictureBox1.Image = Image.FromFile(.FileName)
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream
Me.PictureBox1.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp)
Dim myData(ms.Length) As Byte
ms.Position = 0
ms.Read(myData,Convert.ToInt32(ms.Length))
With Me.m_dtProductPic
Dim dtRow As DataRow
dtRow = .NewRow
dtRow( dPP_Pic ) = myData
dtRow( dPP_No ) = Format(.Rows.Count + 1,000 )
.Rows.Add(dtRow)
End With
End With
End Sub


显示dt里的图片到PictureBox
Private Sub Lg_DisplayProductPic()
If Me.m_cmProductPic.Count = 0 Then Exit Sub
Dim drv1 As DataRowView = CType(m_cmProductPic.Current,DataRowView)

If drv1 Is DBNull.Value Then Exit Sub
Dim myDate() As Byte
myDate = CType(drv1( dPP_Pic ),Byte())

Dim ms As New System.IO.MemoryStream(myDate)
Me.PictureBox1.Image = Image.FromStream(ms)

End Sub
__________________________________________________________________________
先顶再说.
__________________________________________________________________________
我试试先.
谢谢
__________________________________________________________________________
怎么读取图片
读不出来呢
__________________________________________________________________________
先从数据库中取出一条数据到dataTable中来,再用上面的方法
__________________________________________________________________________
Dim drv1 As DataRowView = CType(m_cmProductPic.Current,DataRowView)
这一句老是出错呀
__________________________________________________________________________
Dim m_cmProductPic As CurrencyManager
并指向Gridview里的数据源(dataTable),上述语句就是将Grid中当前行里的图片加载到PictureBox里,领会一下意思吧,不能完全照搬
__________________________________________________________________________
Dim data() As Byte = ds.Tables( employees ).Rows(1).Item( photo )
Dim photo As New IO.MemoryStream(data)
pic.Image.FromStream(photo) //怎么一到这里就出错,提示“无效的参数”?
为何?
__________________________________________________________________________
pic.Image = New Bitmap(photo)
photo.Close()
__________________________________________________________________________
回楼上的,提示一样的错误,使用无效的参数!
以下msdn上代码,在我电脑上试验一下,还是一样的错误。请帮忙!
Dim strCn As String = Data Source= ; & _
Initial Catalog= ;Integrated Security=SSPI Dim cn1 As New sqlConnection(strCn) Dim cmd As New sqlCommand( SELECT employeeId,photo FROM employees ORDER BY employeeId,cn) Dim da1 As New sqlDataAdapter(cmd) Dim ds1 As New DataSet da1.Fill(ds1,BLOBTest ) Dim c As Integer = ds1.Tables( BLOBTest ).Rows.Count If c > 0 Then Dim bytBLOBData() As Byte = _ ds1.Tables( BLOBTest ).Rows(c - 1)( photo ) Dim stmBLOBData As New MemoryStream(bytBLOBData) pic.Image = Image.FromStream(stmBLOBData) //出现错误! End If __________________________________________________________________________ 有没有谁有什么办法呀 __________________________________________________________________________ 基本思路: 1:以数据流存数据库 2:读取还原成图像. __________________________________________________________________________ 老是无效的参数 就如DWM250一样。 __________________________________________________________________________ 存入数据库... Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click With Me.OpenFileDialog1 .Title = 图片位置 .Filter = 所有图片文件|*.gif;*.bmp;*.jpg;*.jpeg;*.png;*.ico End With If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Dim path As String = Me.OpenFileDialog1.FileName Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(path) Me.PictureBox1.Image = img Dim fs As New System.IO.FileStream(path,IO.FileMode.Open,IO.FileAccess.Read) Dim bit(fs.Length) As Byte imgbytes = bit fs.Read(imgbytes,fs.Length) fs.Close() fs = Nothing Dim insql As String = insert into Mange_user values(@img) Dim imgcom As New sqlClient.sqlCommand imgcom.CommandText = insql imgcom.Parameters.Add( @img,sqlDbType.Image).Value = imgbytes ......执行 sql语句..... End If End Sub ----------------------------------------------- 读取..... Dim cc As sqlConnection = con() Dim ccm As sqlCommand = com(strsq) Dim bites() As Byte cc.Open() ccm.Connection = cc Dim readlist As sqlDataReader = ccm.ExecuteReader Do While readlist.Read Dim listtem As New ListViewItem Dim img As System.Drawing.Image If readlist(2) IsNot System.DBNull.Value Then bites = readlist(2) Dim ms As New System.IO.MemoryStream(bites) img = New Bitmap(ms) End If end do __________________________________________________________________________ 哪位大虾扔个demo上来,分不够另外开帖再加! Bitmap 构造函数 (Stream) 从指定的数据流初始化 Bitmap 类的新实例。 [Visual Basic] Public Sub New( _ ByVal stream As Stream _ ) 说明参数是有效的!! __________________________________________________________________________ 你确定数据库photo字段的类型是否为Image类型吗?,检查下... __________________________________________________________________________ 从程序里直接扒出来的,应该不会报错了吧: Private Sub addPic() Dim ms As New MemoryStream() Me.picBoxAdd.Image.Save(ms,Me.picBoxAdd.Image.RawFormat) Dim arrImage() As Byte = ms.GetBuffer 将照片存为字节流 ms.Close() 这里采用代码方式,非可视化方式添加照片到数据库中 Try Dim strsql As String = _ INSERT INTO picture VALUES (@userID,@Picture) Dim cmd As New sqlCommand(strsql,picConnection) With cmd .Parameters.Add(New sqlParameter( @userID,sqlDbType.NVarChar,50)).Value = Me.tBoxUserID.Text .Parameters.Add(New sqlParameter( @Picture,sqlDbType.Image)).Value = arrImage End With picConnection.Open() cmd.ExecuteNonQuery() picConnection.Close() Catch ex As Exception MsgBox(ex.Message,MsgBoxStyle.Critical,sql-Error ) End Try __________________________________________________________________________ 读取: Dim str As String = select userPic from picture where userID = @userID Dim cm As New sqlCommand(str,.picConnection) Dim dr As sqlDataReader cm.Parameters.Add(New sqlParameter( @userID,sqlDbType.NChar,10)).Value = .cBoxShowPic.SelectedValue .picConnection.Open() dr = cm.ExecuteReader dr在读取的时候,connection不能关闭 dr.Read() Dim arrImage() As Byte = CType(dr.Item(0),Byte()) .picConnection.Close() Dim ms As New MemoryStream(arrImage) .picBoxView.Image = Image.FromStream(ms) ms.Close() __________________________________________________________________________ 回ilost1013() ,字段类型为image 回楼上的,我测试了一下代码,照样的错误:使用了无效的参数! 在此处 picBoxView.Image = Image.FromStream(ms) 我真迷糊了。为什么MSDN上的代码也是出同样的错误?难道是我的电脑的问题吗? 各位大虾,谁有测试可行的代码或者其他的方法,扔一段上来。谢谢! __________________________________________________________________________ 建议换台电脑测试下... __________________________________________________________________________ http://support.microsoft.com/kb/317701/zh-cn 请看 缺陷 • 此测试不适用于 Access 和 sql Server 中的罗斯文示例数据库的雇员表中的照片列。存储在照片列中的位图图像用由 Visual Basic 6.0 OLE Container 控件创建的标题信息进行了包装。 • 如果需要使用 Access 数据库测试此代码,则需要在 Access 表中创建一个 OLE Object 类型的列,并使用 Microsoft Jet 4.0 Provider 中的 System.Data.OleDb 名称空间代替 System.Data.sqlClient 名称空间。 所以最好自己建一个Table来验证。 __________________________________________________________________________ 学习 __________________________________________________________________________ 深刻学习中... __________________________________________________________________________ 重装下VS.NET __________________________________________________________________________ to lxcc() 我试了,问题仍旧。 --------------------- 看来只有重装vs.net试试了。 __________________________________________________________________________ to lxcc() 问题仍旧 ----------------------- 看来要重新安装vs.net了。 __________________________________________________________________________

猜你在找的VB相关文章