Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary Imports System.Runtime.SerializationPublic Class PictureStore Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写 dispose 以清理组件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents TextBox3 As System.Windows.Forms.TextBox Friend WithEvents TextBox4 As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents TextBox6 As System.Windows.Forms.TextBox Friend WithEvents Button5 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.Button5 = New System.Windows.Forms.Button Me.Label6 = New System.Windows.Forms.Label Me.TextBox6 = New System.Windows.Forms.TextBox Me.Button4 = New System.Windows.Forms.Button Me.Label4 = New System.Windows.Forms.Label Me.TextBox3 = New System.Windows.Forms.TextBox Me.TextBox4 = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.Button3 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.GroupBox1.SuspendLayout() Me.SuspendLayout() ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.Button5) Me.GroupBox1.Controls.Add(Me.Label6) Me.GroupBox1.Controls.Add(Me.TextBox6) Me.GroupBox1.Controls.Add(Me.Button4) Me.GroupBox1.Controls.Add(Me.Label4) Me.GroupBox1.Controls.Add(Me.TextBox3) Me.GroupBox1.Controls.Add(Me.TextBox4) Me.GroupBox1.Controls.Add(Me.Label3) Me.GroupBox1.Controls.Add(Me.Button3) Me.GroupBox1.Controls.Add(Me.Button2) Me.GroupBox1.Controls.Add(Me.Label2) Me.GroupBox1.Controls.Add(Me.Label1) Me.GroupBox1.Controls.Add(Me.TextBox2) Me.GroupBox1.Controls.Add(Me.TextBox1) Me.GroupBox1.Controls.Add(Me.Button1) Me.GroupBox1.Location = New System.Drawing.Point(8, 8) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(408, 184) Me.GroupBox1.TabIndex = 0 Me.GroupBox1.TabStop = False ' 'Button5 ' Me.Button5.Location = New System.Drawing.Point(320, 152) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(72, 24) Me.Button5.TabIndex = 27 Me.Button5.Text = "读取输出" ' 'Label6 ' Me.Label6.AutoSize = True Me.Label6.Location = New System.Drawing.Point(4, 160) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(60, 17) Me.Label6.TabIndex = 26 Me.Label6.Text = "输出文件:" ' 'TextBox6 ' Me.TextBox6.Location = New System.Drawing.Point(68, 152) Me.TextBox6.Name = "TextBox6" Me.TextBox6.Size = New System.Drawing.Size(224, 21) Me.TextBox6.TabIndex = 25 Me.TextBox6.Text = "C:\Output.bmp" ' 'Button4 ' Me.Button4.Location = New System.Drawing.Point(320, 120) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(72, 24) Me.Button4.TabIndex = 23 Me.Button4.Text = "浏览" ' 'Label4 ' Me.Label4.AutoSize = True Me.Label4.Location = New System.Drawing.Point(4, 128) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(60, 17) Me.Label4.TabIndex = 22 Me.Label4.Text = "索引文件:"
' 'TextBox3 ' Me.TextBox3.Location = New System.Drawing.Point(68, 120) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size(224, 21) Me.TextBox3.TabIndex = 21 Me.TextBox3.Text = "C:\Picture.idx" ' 'TextBox4 ' Me.TextBox4.Location = New System.Drawing.Point(68, 24) Me.TextBox4.Name = "TextBox4" Me.TextBox4.Size = New System.Drawing.Size(96, 21) Me.TextBox4.TabIndex = 20 Me.TextBox4.Text = "" ' 'Label3 ' Me.Label3.AutoSize = True Me.Label3.Location = New System.Drawing.Point(4, 32) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(66, 17) Me.Label3.TabIndex = 19 Me.Label3.Text = "索引编号:" ' 'Button3 ' Me.Button3.Location = New System.Drawing.Point(320, 88) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 18 Me.Button3.Text = "浏览" ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(320, 56) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(72, 24) Me.Button2.TabIndex = 17 Me.Button2.Text = "浏览" ' 'Label2 ' Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(4, 96) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(60, 17) Me.Label2.TabIndex = 16 Me.Label2.Text = "存储文件:" ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(4, 64) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(48, 17) Me.Label1.TabIndex = 15 Me.Label1.Text = "源文件:" ' 'TextBox2 ' Me.TextBox2.Location = New System.Drawing.Point(68, 88) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(224, 21) Me.TextBox2.TabIndex = 14 Me.TextBox2.Text = "C:\Picture.dat" ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(68, 56) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(224, 21) Me.TextBox1.TabIndex = 13 Me.TextBox1.Text = "" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(320, 24) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 12 Me.Button1.Text = "写入存储" ' 'PictureStore ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(424, 197) Me.Controls.Add(Me.GroupBox1) Me.Name = "PictureStore" Me.Text = "PictureStore" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub#End Region Dim sourceFileName As String = "" Dim targetFileName As String = "" Dim indexFileName As String = "" Dim outputFileName As String = "" Dim indexID As String = "" Private Function GetFileLength(ByVal ht As Hashtable, ByVal key As String) As Integer Dim thisValue As Integer Dim nextValue As Integer Dim values As Array values = Array.CreateInstance(GetType(System.Object), ht.Count) ht.Values.CopyTo(values, 0) Dim pos As Integer thisValue = CInt(ht(key)) Array.Sort(values) pos = Array.IndexOf(values, ht(key)) If values.Length - 1 = pos Then Return -1 End If nextValue = values(pos + 1) Return (nextValue - thisValue) End Function '读取文件时需提供索引编号、源文件、存储文件、输出文件 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click outputFileName = Me.TextBox6.Text.Trim targetFileName = Me.TextBox2.Text.Trim indexFileName = Me.TextBox3.Text.Trim indexID = Me.TextBox4.Text.Trim If Me.outputFileName.Length = 0 Or Me.targetFileName.Length = 0 Or _ Me.indexFileName.Length = 0 Or Me.indexID.Length = 0 Then MsgBox("参数不全") Exit Sub End If Dim fs As FileStream '读入索引文件构建HashTable Dim ht As Hashtable If File.Exists(indexFileName) Then ht = Deserialize(indexFileName) If ht Is Nothing Then MsgBox("索引文件生成索引失败") Exit Sub End If Else MsgBox("索引文件不存在") Exit Sub End If If Not (ht.Contains(indexID)) Then MsgBox("不存在的索引号,请重新输入索引号") Exit Sub End If Dim fileLength As Integer fileLength = Me.GetFileLength(ht, indexID) '读入存储文件 If File.Exists(targetFileName) Then fs = New FileStream(targetFileName, FileMode.Open) Else MsgBox("存储文件不存在") Exit Sub End If If fileLength = -1 Then '如果为最后一个文件,则用文件长度减去它保存的初始位置值 fileLength = CInt(fs.Length - ht(indexID)) End If '输出文件 Dim ByteArray(fileLength - 1) As Byte fs.Seek(CInt(ht(indexID)), SeekOrigin.Begin) fs.Read(ByteArray, 0, fileLength) fs.Close() If File.Exists(outputFileName) Then MsgBox("输出文件已存在") Exit Sub Else fs = New FileStream(outputFileName, FileMode.CreateNew) End If fs.Write(ByteArray, 0, ByteArray.Length) fs.Close() MsgBox("读取输出成功") End Sub
'写入文件时需提供索引编号、源文件、存储文件、索引文件 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click sourceFileName = Me.TextBox1.Text.Trim targetFileName = Me.TextBox2.Text.Trim indexFileName = Me.TextBox3.Text.Trim indexID = Me.TextBox4.Text.Trim If Me.sourceFileName.Length = 0 Or Me.targetFileName.Length = 0 Or _ Me.indexFileName.Length = 0 Or Me.indexID.Length = 0 Then MsgBox("参数不全") Exit Sub End If '读入索引文件构建HashTable Dim fs As FileStream Dim ht As Hashtable If File.Exists(indexFileName) Then ht = Deserialize(indexFileName) If ht Is Nothing Then MsgBox("索引文件生成索引失败") Exit Sub End If Else ht = New Hashtable End If If ht.Contains(indexID) Then MsgBox("已存在的索引号,请重新输入索引号") Exit Sub End If '读入存储文件 If File.Exists(targetFileName) Then fs = New FileStream(targetFileName, FileMode.Open) Else fs = New FileStream(targetFileName, FileMode.CreateNew) End If '创建文件索引 ht(indexID) = fs.Length fs.Close() If Not (Serialize(ht, indexFileName)) Then MsgBox("创建索引文件失败") Exit Sub End If '读入图片 If File.Exists(sourceFileName) Then fs = New FileStream(sourceFileName, FileMode.Open) Else MsgBox("源文件不存在") Exit Sub End If Dim ByteArray(fs.Length - 1) As Byte fs.Read(ByteArray, 0, fs.Length) fs.Close() '写入存储文件 fs = New FileStream(targetFileName, FileMode.Append, FileAccess.Write) fs.Write(ByteArray, 0, ByteArray.Length) fs.Close() MsgBox("写入存储成功") End Sub Private Sub PictureStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub '序列化HashTable Private Function Serialize(ByVal ht As Hashtable, ByVal indexFile As String) As Boolean Dim fs As New FileStream(indexFile, FileMode.OpenOrCreate) Dim formatter As New BinaryFormatter Try formatter.Serialize(fs, ht) Return True Catch e As SerializationException Return False Finally fs.Close() End Try End Function '反序列化HashTable Private Function Deserialize(ByVal indexFile As String) As Hashtable Dim ht As Hashtable = Nothing Dim fs As New FileStream(indexFile, FileMode.Open) Try Dim formatter As New BinaryFormatter ht = DirectCast(formatter.Deserialize(fs), Hashtable) Catch e As SerializationException Finally fs.Close() End Try Return ht End Function Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fileDialog As New OpenFileDialog fileDialog.ShowDialog() Me.TextBox1.Text = fileDialog.FileName fileDialog.Dispose() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim fileDialog As New OpenFileDialog fileDialog.ShowDialog() Me.TextBox2.Text = fileDialog.FileName fileDialog.Dispose() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim fileDialog As New OpenFileDialog fileDialog.ShowDialog() Me.TextBox2.Text = fileDialog.FileName fileDialog.Dispose() End SubEnd Class
Stream stream = new FileStream("E:\\Image.xml",FileMode.Create,FileAccess.Write,FileShare.None);
SoapFormatter f = new SoapFormatter();
Image img = Image.FromFile("E:\\Image.bmp");
f.Serialize(stream,img);
stream.Close();
定义一个图片文件流do
将文件名和当前位置记录到hashtable中
以流方式打开图片文件
写入
while(所有文件是否写完) 打开文件
写入hashtable
写入一个标记表示hashtable结束
图片文件流
关闭保存文件其实这样写跟用数据库差不多,不过数据库它优化了很多查询
你说的每张图片的开始很结束做个记号来标志我不知道,
但是如果你只是想将所有图片的格式(包括文件头,版本号,作者等等)写入这个文件,完全可以在ArrayList 中的一开始加入一个结构数组,把文件的文件头,版本号,作者等写入结构,然后放在ArrayList中一起序列化,读取图片的时候先读结构来知道图片文件的信息。至于用专用软件打开,你自己写个不就ok了。
循环
以流方式打开图片文件
判断
写完就
打开文件
写入hashtable
关闭
保存
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.SerializationPublic Class PictureStore
Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 " Public Sub New()
MyBase.New() '该调用是 Windows 窗体设计器所必需的。
InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
Friend WithEvents Button5 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.Button5 = New System.Windows.Forms.Button
Me.Label6 = New System.Windows.Forms.Label
Me.TextBox6 = New System.Windows.Forms.TextBox
Me.Button4 = New System.Windows.Forms.Button
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.Button3 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.Button5)
Me.GroupBox1.Controls.Add(Me.Label6)
Me.GroupBox1.Controls.Add(Me.TextBox6)
Me.GroupBox1.Controls.Add(Me.Button4)
Me.GroupBox1.Controls.Add(Me.Label4)
Me.GroupBox1.Controls.Add(Me.TextBox3)
Me.GroupBox1.Controls.Add(Me.TextBox4)
Me.GroupBox1.Controls.Add(Me.Label3)
Me.GroupBox1.Controls.Add(Me.Button3)
Me.GroupBox1.Controls.Add(Me.Button2)
Me.GroupBox1.Controls.Add(Me.Label2)
Me.GroupBox1.Controls.Add(Me.Label1)
Me.GroupBox1.Controls.Add(Me.TextBox2)
Me.GroupBox1.Controls.Add(Me.TextBox1)
Me.GroupBox1.Controls.Add(Me.Button1)
Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(408, 184)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(320, 152)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(72, 24)
Me.Button5.TabIndex = 27
Me.Button5.Text = "读取输出"
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(4, 160)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(60, 17)
Me.Label6.TabIndex = 26
Me.Label6.Text = "输出文件:"
'
'TextBox6
'
Me.TextBox6.Location = New System.Drawing.Point(68, 152)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(224, 21)
Me.TextBox6.TabIndex = 25
Me.TextBox6.Text = "C:\Output.bmp"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(320, 120)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(72, 24)
Me.Button4.TabIndex = 23
Me.Button4.Text = "浏览"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(4, 128)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(60, 17)
Me.Label4.TabIndex = 22
Me.Label4.Text = "索引文件:"
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(68, 120)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(224, 21)
Me.TextBox3.TabIndex = 21
Me.TextBox3.Text = "C:\Picture.idx"
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(68, 24)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(96, 21)
Me.TextBox4.TabIndex = 20
Me.TextBox4.Text = ""
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(4, 32)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(66, 17)
Me.Label3.TabIndex = 19
Me.Label3.Text = "索引编号:"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(320, 88)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(72, 24)
Me.Button3.TabIndex = 18
Me.Button3.Text = "浏览"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(320, 56)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 24)
Me.Button2.TabIndex = 17
Me.Button2.Text = "浏览"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(4, 96)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(60, 17)
Me.Label2.TabIndex = 16
Me.Label2.Text = "存储文件:"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(4, 64)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 17)
Me.Label1.TabIndex = 15
Me.Label1.Text = "源文件:"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(68, 88)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(224, 21)
Me.TextBox2.TabIndex = 14
Me.TextBox2.Text = "C:\Picture.dat"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(68, 56)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(224, 21)
Me.TextBox1.TabIndex = 13
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(320, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 12
Me.Button1.Text = "写入存储"
'
'PictureStore
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(424, 197)
Me.Controls.Add(Me.GroupBox1)
Me.Name = "PictureStore"
Me.Text = "PictureStore"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False) End Sub#End Region Dim sourceFileName As String = ""
Dim targetFileName As String = ""
Dim indexFileName As String = ""
Dim outputFileName As String = ""
Dim indexID As String = "" Private Function GetFileLength(ByVal ht As Hashtable, ByVal key As String) As Integer
Dim thisValue As Integer
Dim nextValue As Integer
Dim values As Array
values = Array.CreateInstance(GetType(System.Object), ht.Count)
ht.Values.CopyTo(values, 0) Dim pos As Integer
thisValue = CInt(ht(key))
Array.Sort(values)
pos = Array.IndexOf(values, ht(key))
If values.Length - 1 = pos Then
Return -1
End If
nextValue = values(pos + 1)
Return (nextValue - thisValue)
End Function '读取文件时需提供索引编号、源文件、存储文件、输出文件
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
outputFileName = Me.TextBox6.Text.Trim
targetFileName = Me.TextBox2.Text.Trim
indexFileName = Me.TextBox3.Text.Trim
indexID = Me.TextBox4.Text.Trim
If Me.outputFileName.Length = 0 Or Me.targetFileName.Length = 0 Or _
Me.indexFileName.Length = 0 Or Me.indexID.Length = 0 Then
MsgBox("参数不全")
Exit Sub
End If Dim fs As FileStream '读入索引文件构建HashTable
Dim ht As Hashtable
If File.Exists(indexFileName) Then
ht = Deserialize(indexFileName)
If ht Is Nothing Then
MsgBox("索引文件生成索引失败")
Exit Sub
End If
Else
MsgBox("索引文件不存在")
Exit Sub
End If If Not (ht.Contains(indexID)) Then
MsgBox("不存在的索引号,请重新输入索引号")
Exit Sub
End If Dim fileLength As Integer
fileLength = Me.GetFileLength(ht, indexID) '读入存储文件
If File.Exists(targetFileName) Then
fs = New FileStream(targetFileName, FileMode.Open)
Else
MsgBox("存储文件不存在")
Exit Sub
End If If fileLength = -1 Then
'如果为最后一个文件,则用文件长度减去它保存的初始位置值
fileLength = CInt(fs.Length - ht(indexID))
End If '输出文件
Dim ByteArray(fileLength - 1) As Byte
fs.Seek(CInt(ht(indexID)), SeekOrigin.Begin)
fs.Read(ByteArray, 0, fileLength)
fs.Close() If File.Exists(outputFileName) Then
MsgBox("输出文件已存在")
Exit Sub
Else
fs = New FileStream(outputFileName, FileMode.CreateNew)
End If
fs.Write(ByteArray, 0, ByteArray.Length)
fs.Close()
MsgBox("读取输出成功")
End Sub
'写入文件时需提供索引编号、源文件、存储文件、索引文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sourceFileName = Me.TextBox1.Text.Trim
targetFileName = Me.TextBox2.Text.Trim
indexFileName = Me.TextBox3.Text.Trim
indexID = Me.TextBox4.Text.Trim
If Me.sourceFileName.Length = 0 Or Me.targetFileName.Length = 0 Or _
Me.indexFileName.Length = 0 Or Me.indexID.Length = 0 Then
MsgBox("参数不全")
Exit Sub
End If
'读入索引文件构建HashTable
Dim fs As FileStream
Dim ht As Hashtable If File.Exists(indexFileName) Then
ht = Deserialize(indexFileName)
If ht Is Nothing Then
MsgBox("索引文件生成索引失败")
Exit Sub
End If
Else
ht = New Hashtable
End If If ht.Contains(indexID) Then
MsgBox("已存在的索引号,请重新输入索引号")
Exit Sub
End If '读入存储文件
If File.Exists(targetFileName) Then
fs = New FileStream(targetFileName, FileMode.Open)
Else
fs = New FileStream(targetFileName, FileMode.CreateNew)
End If '创建文件索引
ht(indexID) = fs.Length
fs.Close()
If Not (Serialize(ht, indexFileName)) Then
MsgBox("创建索引文件失败")
Exit Sub
End If '读入图片
If File.Exists(sourceFileName) Then
fs = New FileStream(sourceFileName, FileMode.Open)
Else
MsgBox("源文件不存在")
Exit Sub
End If
Dim ByteArray(fs.Length - 1) As Byte
fs.Read(ByteArray, 0, fs.Length)
fs.Close() '写入存储文件
fs = New FileStream(targetFileName, FileMode.Append, FileAccess.Write)
fs.Write(ByteArray, 0, ByteArray.Length)
fs.Close() MsgBox("写入存储成功")
End Sub Private Sub PictureStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub '序列化HashTable
Private Function Serialize(ByVal ht As Hashtable, ByVal indexFile As String) As Boolean
Dim fs As New FileStream(indexFile, FileMode.OpenOrCreate)
Dim formatter As New BinaryFormatter
Try
formatter.Serialize(fs, ht)
Return True
Catch e As SerializationException
Return False
Finally
fs.Close()
End Try
End Function '反序列化HashTable
Private Function Deserialize(ByVal indexFile As String) As Hashtable
Dim ht As Hashtable = Nothing
Dim fs As New FileStream(indexFile, FileMode.Open)
Try
Dim formatter As New BinaryFormatter
ht = DirectCast(formatter.Deserialize(fs), Hashtable)
Catch e As SerializationException Finally
fs.Close()
End Try
Return ht
End Function Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim fileDialog As New OpenFileDialog
fileDialog.ShowDialog()
Me.TextBox1.Text = fileDialog.FileName
fileDialog.Dispose()
End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim fileDialog As New OpenFileDialog
fileDialog.ShowDialog()
Me.TextBox2.Text = fileDialog.FileName
fileDialog.Dispose()
End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim fileDialog As New OpenFileDialog
fileDialog.ShowDialog()
Me.TextBox2.Text = fileDialog.FileName
fileDialog.Dispose()
End SubEnd Class