asp.net 图片和十六进制互转

来源:互联网 发布:java有gc内存溢出 编辑:程序博客网 时间:2024/06/05 15:08

asp.net(vb)图片转十六进制字符串,且保存在文本文件中

代码:

        Dim fs As FileStream = Nothing
        Dim br As BinaryReader = Nothing
        Dim sw As StreamWriter = Nothing
        Try
            fs = New FileStream("11.jpg", FileMode.Open, FileAccess.Read)
            br = New BinaryReader(fs)
            sw = New StreamWriter("11.txt")
            Dim length As Int16 = fs.Length
            While length > 0
                Dim tempbyte As Byte = br.ReadByte
                Dim tempint As Int16 = Convert.ToInt32(tempbyte)
                Dim tempstr As String = Convert.ToString(tempint, 16)
                sw.WriteLine(tempstr)
                length = length - 1

            End While
            MsgBox("完成")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            sw.Close()
            br.Close()
            fs.Close()
        End Try

 

从记事本中读取十六进制,还原成图片

       Dim fs As FileStream = Nothing
        Dim bw As BinaryWriter = Nothing
        Dim sr As StreamReader = Nothing
        Try
            fs = New FileStream("22.jpg", FileMode.Create, FileAccess.Write)
            bw = New BinaryWriter(fs)
            sr = New StreamReader("11.txt")
            While sr.Peek <> -1
                Dim tempstr As String = sr.ReadLine
                Dim tempint As Int16 = Convert.ToInt16(tempstr, 16)
                Dim tempbyte As Byte = Convert.ToByte(tempint)
                bw.Write(tempbyte)

            End While
            MsgBox("ok")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            sr.Close()
            bw.Close()
            fs.Close()
        End Try

原创粉丝点击