关于图片在流中存取的问题,最近比较烦……

QQRN 2002-12-14 05:18:57
一个图片,保存到流,就算是16色的ICON最少也有300多字节,但是用IMAGE读流时候只读几十,图片没能显示,晕到,请教各位兄弟姐妹……
...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
QQRN 2002-12-28
  • 打赏
  • 举报
回复
555颜色变调了,倒霉呀,没办法了吗?
不管结贴,再发贴问如何把ICON对象存为ICO文件好了
xumahua 2002-12-16
  • 打赏
  • 举报
回复
'保存图片,将文件读到流中,对于怎么将pic的值写到库中略
Dim myfilestream As New System.IO.FileStream(OpenFile.FileName, IO.FileMode.Open)
Dim pic() As Byte
Dim pic as (myfilestream.Length - 1)
myfilestream.Read(pic, 0, myfilestream.Length)
myfilestream.Close()

'显示图片假定你吧数据读到pic字符数组中

Dim ms As New MemoryStream(pic) '定义个memory流

PictureBox1.Image = Image.FromStream(ms)

QQRN 2002-12-14
  • 打赏
  • 举报
回复
谢谢上面的两位老兄,因为我是要保存ICON,存为BMP的话会变样的,这个方法我已经考虑过,因为行不通,所以才问用流的方法,毕竟。NET的IMAGE有保存到流里和从流里读出数据的方法,只是我不懂得如何用,MSDN里说得太简单了。
刚才网络断线,信息没有及时发送,对不起了
QQRN 2002-12-14
  • 打赏
  • 举报
回复
如果那位提供数据库存取ICON的方法也可以,但是不要告诉我用文件转存,我试过,ICON变样了,555555555
Acony 2002-12-14
  • 打赏
  • 举报
回复
请看下面读写文件的代码实现(图片也是一样的)

Public class ReadWrite

(首先,需要获得你所指定的文件大小并将其转换为字节,因此要将数字乘以 1024。得到正确的合计后,指定在 OpenOrCreate“文件模式”中打开该文件。这意味着如果该文件存在,则打开它,如果它不存在,则创建它。生成引用后,可以通过将 Stream 的长度设置为零来确保文件为空)

Public Shared Function Go ( fileSize aS double ) As String

'声明 StreamReader,用于访问文件
Dim strWriter As StringWriter = New StringWriter()
Console.SetOut(strWriter)

Dim origTestSize As double = fileSize

Dim testSize As double = origTestSize * 1024

Dim fs As FileStream = New FileStream("Filename", FileMode.OpenOrCreate)
fs.SetLength(0)

(准备好填充文件后,可以开始写进程。首先创建一个 BinaryWriter 对象。需要确保编写器指向文件的开始,因此请使用 Seek 方法移动到文件前面,然后创建该文件,向编写器写入指定的字节数,由于预先并不知道要生成的文件将有多大,因此一次只向文件写入一个字节)

Dim w As BinaryWriter = New BinaryWriter(fs) ' 创建 BinaryWriter

w.BaseStream.Seek(0,SeekOrigin.Begin) ' 搜索文件的开始

Console.Write ("正在写入文件...")

(在向文件写入第一个字节时,将从在其中操作的“环境”中获取当前 TickCount。代码将使用它来确定写进程是何时开始的)

Dim beginWrite As Integer = Environment.TickCount ' 获取开始时间

Dim intCount As Integer

For intCount = 0 To testSize - 1 ' 依次通过文件,写字节
w.Write( CType(1, byte)) ' 执行写操作
Next intCount


Console.WriteLine ()

fs.Flush()

Dim endWrite As Integer = Environment.TickCount ' 获取结束时间

Console.WriteLine ("对于写操作...")
Console.WriteLine ("开始时间:{0}", beginWrite.ToString())
Console.WriteLine ("结束时间:{0}", endWrite.ToString())
Console.WriteLine ("运行时间:{0} 毫秒", endWrite-beginWrite) ' 时间跨度为毫秒
Dim thruputWrite As double = (CType(origTestSize, double) / _
((CType(endWrite, double) - CType(beginWrite, double))) * 1000)
Console.WriteLine ("数据吞吐量:{0} MB/秒", thruputWrite)

Dim r As BinaryReader = New BinaryReader(fs) ' 创建 BinaryReader

w.BaseStream.Seek(0,SeekOrigin.Begin) '将文件指针设置到文件的开始
Dim dummyInt As Integer

Dim beginRead As Integer = Environment.TickCount ' 获取开始时间

For intCount = 0 To testSize - 1 ' 依次通过,读取字节
dummyInt = r.ReadByte() ' 执行读取操作
Next intCount

fs.Flush()

Dim endRead As Integer = Environment.TickCount ' 确定结束时间

Console.WriteLine ()
Console.WriteLine ("对于读操作...")
Console.WriteLine ("开始时间:{0}", beginRead.ToString())
Console.WriteLine ("结束时间:{0}", endRead.ToString())
Console.WriteLine ("运行时间:{0}", endRead-beginRead) ' 时间跨度为毫秒
Dim thruputRead As double = (CType(origTestSize, double) / _
((CType(endRead, double) - CType(beginRead, double))) * 1000)
Console.WriteLine ("数据吞吐量:{0} MB/秒", thruputRead)

fs.Close() ' 关闭文件

System.IO.File.Delete("data.bin") ' 删除文件

return strWriter.ToString()
End Function
End Class

Sub btnCreate_Click(Src As Object, E As EventArgs)

If Convert.ToDouble( txtFileSize.Text ) > 10000 Then
lblOutput.Text = "可以指定的数目的上限为 10000"
Else
If Convert.ToDouble( txtFileSize.Text ) < 1
lblOutput.Text = "可以指定的数目的下限为 1"
Else
lblOutput.Text = ReadWrite.Go( Double.Parse(txtFileSize.Text) )
End If
End If
End Sub
xumahua 2002-12-14
  • 打赏
  • 举报
回复
看看下面是图片的存取代码:
以MS自带的数据库Northwnd为例,其中有个表是Categories,有四个四段,其中有一个是Image类型的Picture字段.我们首先添加一张bmp图片到最后一行的Picture中,然后在读出来显示到Image控件中.

添加一个SqlDataAdapter1,用向导设置联接数据库为Northwnd,SQL语句为SELECT [Category ID], [Category Name], Description, Picture FROM Categories.生成一个数据集为dataset1. 然后添加两个按钮分别表示写图片到数据库和读数据库,还有一个Image控件用于显示图片.

添加以下代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
End Sub

'从数据库读取图片暂时存储为monkey.bmp,然后加载到image控件里面.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click
Try
Dim data As Byte() = DataSet11.Tables(0).Rows(7).Item(3)
Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
PictureBox1.Image = New Bitmap(Application.StartupPath & "\monkey.bmp")
Catch
End Try
End Sub


'把C:\6.bmp写入库中,你可以改为自己的图片.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click
Dim myfilestream As New System.IO.FileStream("c:\6.bmp", IO.FileMode.Open)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
DataSet11.Tables(0).Rows(7).Item(3) = data
SqlDataAdapter1.Update(DataSet11.GetChanges())
End Sub
visualcpu 2002-12-14
  • 打赏
  • 举报
回复
mark

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧