I want to convert jpeg's to Hex but I don't really seem to get it right.
The code I use is the following:
Private Function tmpGetByteFileFromDisk(ByVal FilePath As String) As Byte()
Try
Dim fs As FileStream = New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim i As Integer
Dim photo As String
br.Read()
Dim p As String
For i = 0 To 40697
p = Hex(br.ReadByte)
If Len(p) = 1 Then
photo = photo & "0" & p
Else
photo = photo & p
End If
Next
TextBox1.Text = photo
br.Close()
fs.Close()
I want this done to insert the image into a rtf-document using the
streamwriter object, this way I can generate rtf-documents without all the
overhead of starting the word.application.
Suggestions greatly appreciated.
>I want to convert jpeg's to Hex but I don't really seem to get it right.
> The code I use is the following:
[quoted text clipped - 26 lines]
>
> Suggestions greatly appreciated.
Here's a suggestion:
Private Function FileToHex(ByVal FilePath As String) As String
Dim fs As FileStream = New FileStream(FilePath, _
FileMode.Open, _
FileAccess.Read)
Dim buf(CInt(fs.Length)) As Byte
fs.Read(buf, 0, buf.Length)
fs.Close()
Static hexChars() As Char = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c,
"6"c, _
"7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c,
"E"c, "F"c}
Dim sb As New Text.StringBuilder(buf.Length * 2)
For Each b As Byte In buf
Dim n1 As Integer = b >> 4
Dim n2 As Integer = b And 15
sb.Append(hexChars(n1))
sb.Append(hexChars(n2))
Next
Return sb.ToString
End Function
David
dontGetStuckLikeMe - 07 Aug 2005 17:46 GMT
Thank's, now it's all working!!!
Hi,
I'm not familiar with images in rtf but this page may be of interest:
http://dotnet.org.za/pieter/archive/2005/03/29/15961.aspx

Signature
Happy coding!
Morten Wennevik [C# MVP]