|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem to write a good serie of bytes in a file.code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes written in file are not the sent bytes. Copy and paste the following lines to observe my problem. What can I do to resolve problem ? Only System.Text.Encoding.ASCII write the same number of bytes, but not the good bytes. Someone can help me. Thanks by advance. Public Class Form1 Dim TextBox1 As New TextBox Dim TextBox2 As New TextBox Dim Label1 As New Label Dim Label2 As New Label Dim Label3 As New Label Dim Label4 As New Label Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Size = New System.Drawing.Size(800, 500) Label1.Location = New System.Drawing.Size(20, 13) Label1.Text = "Data to write" Me.Controls.Add(Label1) Label2.Location = New System.Drawing.Size(20, 40) Label2.Text = "Data written" Me.Controls.Add(Label2) Label3.Location = New System.Drawing.Size(350, 13) 'Label3.Text = "Data written" Me.Controls.Add(Label3) Label4.Location = New System.Drawing.Size(350, 40) 'Label4.Text = "Data written" Me.Controls.Add(Label4) TextBox1.Size = New System.Drawing.Size(200, 20) TextBox1.Location = New System.Drawing.Size(120, 13) Me.Controls.Add(TextBox1) TextBox2.Size = New System.Drawing.Size(200, 20) TextBox2.Location = New System.Drawing.Size(120, 40) Me.Controls.Add(TextBox2) Dim button1 As New Button button1.Text = "BinaryWriter encoding ASCII" button1.Size = New System.Drawing.Size(200, 20) button1.Location = New System.Drawing.Size(550, 13) AddHandler button1.Click, AddressOf Button1_Click Me.Controls.Add(button1) End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim Temp As String = "ÿØÿá" TextBox1.Text = Temp Label3.Text = "Length to write : " & Temp.Length.ToString & " octets)" Dim fullFileName As String = "c:\converted music\test1.jpg" Dim sw As System.IO.StreamWriter sw = New System.IO.StreamWriter(fullFileName, False, System.Text.Encoding.ASCII) sw.Write(Temp) sw.Close() Dim sr As System.IO.StreamReader sr = New System.IO.StreamReader(fullFileName, System.Text.Encoding.ASCII) Do While Not sr.EndOfStream TextBox2.Text = TextBox2.Text & Chr(sr.Read()) Loop Label4.Text = "Length written : " & TextBox2.Text.Length.ToString & " octets)" sr.Close() End Sub End Class Keep in mind that ASCII only contains codes from 0 to 127. The
characters you are attempting to write are not part of the ASCII Encoding. You might try Encoding.Default or Encoding.UTF8 to see if that helps. Or use the Encoding for the language you are trying to write. Hope this helps. Thanks for your response.
I explain why I must do that. 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. ----------------------------------------- PS : In VBA of Access I can do that like this (and that functions perfectly) : Dim lFile1 As LongDim Temp1 as string lFile1 = FreeFile Open nomFileImageWithChemin For Binary As #lFile1 Put #lFile1, , temp Close #lFile1 ------------------------------------------- Great thanks for your attention. "Chris Dunaway" <dunaw***@gmail.com> a écrit dans le message de news: 1139079101.049962.74***@g44g2000cwa.googlegroups.com...Show quoteHide quote > Keep in mind that ASCII only contains codes from 0 to 127. The > characters you are attempting to write are not part of the ASCII > Encoding. You might try Encoding.Default or Encoding.UTF8 to see if > that helps. Or use the Encoding for the language you are trying to > write. > > Hope this helps. >
Show quote
Hide quote
> I write a program in Visual Studio, in vb, bound to an Access Database. Philip,> 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. 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 Thanks for your interest.
1. I launch you routine, but I have an error on PictureBox1.Image = Image.FromStream(myMemoryStream, True) The error is 'Parameter is not valid'. Do you know why ? 2. I disactivated this problematic line only to see if the remaining part functions well. And there is a another problem. When BinaryReader read 'in.jpg', the array myByteArray is 25864 long (It's good, it's the length of 'in.jpg'). But when UTF16 codes myByteArray, myByteArray loses some bytes and this array has now only 25092 elements. So 'out.jpg' is only 25092 bytes long. And 'in.jpg' is good and visible in pictureBox, but not 'out.jpg'. I'm a very happy because you approach from truth ands you have interest for my problem. Can you spend again a lot of time to study these problems ? Thanks by advance if it's possible. "Stan Smith" <ssm***@adsprogramming.com> a écrit dans le message de news: %23TUOhehKGHA.1***@TK2MSFTNGP12.phx.gbl...Show quoteHide quote >> 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. > > 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 > > 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" <ssm***@adsprogramming.com> a écrit dans le message de news: %23TUOhehKGHA.1***@TK2MSFTNGP12.phx.gbl...Show quoteHide quote >> 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. > > 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 > >
Printing using margins
Subclassed Textbox OnLeave Overrides Additional information: Cast from string "" to type 'Double' is not valid. error Visual Basic 2005 Variable declarations.... in VB.Net PostMessage ComboBox IIS Making Bitmaps and Graphics objects - two ways filtering data view with row filter using boolean value combobox displays [Namespace].[ClassName] |
|||||||||||||||||||||||