I am the happiest man in the world !
Your routine functions ! Just I cut some lines :
Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\in.jpg"))
Dim myByteArray As Byte() =
myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)
myBinaryReader.Close()
' Convert the byte array to a MemoryStream
Dim myMemoryStream As MemoryStream
myMemoryStream = New MemoryStream(myByteArray, 0,
myByteArray.Length)
myMemoryStream.Write(myByteArray, 0, myByteArray.Length)
Debug.Print(myMemoryStream.Length)
' Display the image from the MemoryStream
PictureBox1.Image = Image.FromStream(myMemoryStream, True)
' Write the byte array to a new disk file
Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\out.jpg"))
myBinaryWriter.Write(myByteArray)
myBinaryWriter.Close()
Any conversion ASCII ou UTF16
Just one thing : in your routine you begin with a filestream, but I begin
with a string.
I am obliged to transform strin in Array of bytes without any encoder.
I do that :
Dim strImage As String = row.Cells("picture_field").Value.ToString
Dim myByteArray(strImage.Length) As Byte
Dim i As Integer
For i = 0 To strImage.Length - 1
myByteArray(i) = Asc(strImage.Chars(i))
Next
Do you know, to finish to resolve my problem, a better way to do that ?
I am sure that a function exists but I don't know which.
It will be my last question.
Thanks four your help.
Philip
"Stan Smith" <ssmith@adsprogramming.com> a écrit dans le message de news:
%23TUOhehKGHA.1532@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
>> I write a program in Visual Studio, in vb, bound to an Access Database.
>> Some memo field contains exact copy, byte to byte, of 'jpg' files, each
>> file beeing obviously shorter then 65 k.
>> What I search to do if to write on a file the bytes loaded in memo file.
>> I put bytes of memo file in a string and I try to write the bytes in a
>> file with 'jpg' extension.
>> If I succeed, I can use image.fromfile(jpgfile) to show the file in a
>> picture box.
>>
>> BUT if I use encoding.ASCII, I loose all bytes greater than 127, and I
>> keep the same number of bytes than the number of bytes of the memo file
>> (and string). Obviously, And the jpgfile resulting doesn't work. because
>> I lost all the values greater than 127, replaces by chr63 ='?'
>>
>> BUT if I use UTF8, bytes are coded in a such way that the binary values
>> of the file written are not the same, byte by byte, than the binary
>> values of the string because bytes greater than 127 are coded on 2 bytes,
>> I think. And the number of bytes is not the same. And the jpgfile
>> resulting doesn't work.
>>
>> What I search is a way to write all bytes in a file keeping the sale
>> number of bytes and writing on a byte the values between 0 and 256.
>> Is it possible ? If it is possible and you have solution, I'm the hapiest
>> man in the world.[/color]
>
> Philip,
>
> See if this code will increase your happiness :-). The secret is encoding
> and decoding using UTF-16.
>
> You will need:
>
> Imports System.IO
> Imports System.Text
>
> a PictureBox named "PictureBox1", a button named "Button1" on your form
> and a jpeg file named "In.jpg" in C:\Temp. This code doesn't do any error
> handling but I think it will give you a good start.
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> ' Read a jpg into a byte array
>
> Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\Temp\In.jpg"))
>
> Dim myByteArray As Byte() =
> myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)
>
> myBinaryReader.Close()
>
> ' Encode the byte array using UTF-16 to a string for storage (in a memo
> field?)
>
> Dim myString As String
>
> Dim myUTF16 As New UnicodeEncoding
>
> myString = myUTF16.GetString(myByteArray)
>
> ' Decode the string using UTF-16 back to a byte array
>
> myByteArray = myUTF16.GetBytes(myString)
>
> ' Convert the byte array to a MemoryStream
>
> Dim myMemoryStream As MemoryStream
>
> myMemoryStream = New MemoryStream(myByteArray, 0, myByteArray.Length)
>
> myMemoryStream.Write(myByteArray, 0, myByteArray.Length)
>
> ' Display the image from the MemoryStream
>
> PictureBox1.Image = Image.FromStream(myMemoryStream, True)
>
> ' Write the byte array to a new disk file
>
> Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\Temp\Out.jpg"))
>
> myBinaryWriter.Write(myByteArray)
>
> myBinaryWriter.Close()
>
> End Sub
>
> Stan
>
> Stan Smith
> ADS Programming Services
> 2320 Highland Avenue South
> Suite 290
> Birmingham, AL 35205
> 205-222-1661
>
www.adsprogramming.com
> ssmith_at_adsprogramming.com
>
>[/color]