473,408 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

StreamWriter and BinaryWriter (same problem better explained)

If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
sw.Write(temp)
sw.Close()

the resulting file 'Text.txt' has the same length than the string 'temp'.
Same result with I Use BinayWriter.

BUT if 'temp' contains others characters than readable character, then the
result file has not the same length than the string.

So, if temp contains "ÿØÿá" (that is to say a string concanated with the 4
characters chr(255) & chr(216) & chr(255) and chr(225)), the file writen is
8 octets long, and not 4 octets like the string (twice more in the file than
in the string). Same problem with BinaryWriter.

My problem is that I try to write, in a new 'jpg' file, some data loaded in
a memo file. This memo contains the exact image of a 'jpg' file which I must
reproduce.
So with the method exposed above, result cannot good, because this method
does'nt write exactly the octets.

In Access VBA, with the method "open for binary", all is good. I can
reconstruc the 'jpg' file from the memo, then I can see image in a
picturebox.

So I think that I don't use the good method in Visual Studio 2005 to copy
this string on a file without any change.

Can someone help me and tell me the methos to do that ?

Thanks for response.

Feb 3 '06 #1
9 8139
If I understand you correctly you are trying to read an image from a
database, in which case you could try:

Dim ms as IO.MemoryStream
ms = New IO.MemoryStream(Row.PictureData)
PictureBox1.Image = New Bitmap(ms)

Hope this helps,

Martin

"philip" <ph**@philippe.com> wrote in message
news:43**********************@news.wanadoo.fr...
If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
sw.Write(temp)
sw.Close()

the resulting file 'Text.txt' has the same length than the string 'temp'.
Same result with I Use BinayWriter.

BUT if 'temp' contains others characters than readable character, then the
result file has not the same length than the string.

So, if temp contains "ÿØÿá" (that is to say a string concanated with the
4 characters chr(255) & chr(216) & chr(255) and chr(225)), the file writen
is 8 octets long, and not 4 octets like the string (twice more in the file
than in the string). Same problem with BinaryWriter.

My problem is that I try to write, in a new 'jpg' file, some data loaded
in a memo file. This memo contains the exact image of a 'jpg' file which I
must reproduce.
So with the method exposed above, result cannot good, because this method
does'nt write exactly the octets.

In Access VBA, with the method "open for binary", all is good. I can
reconstruc the 'jpg' file from the memo, then I can see image in a
picturebox.

So I think that I don't use the good method in Visual Studio 2005 to copy
this string on a file without any change.

Can someone help me and tell me the methos to do that ?

Thanks for response.

Feb 3 '06 #2
From your message, I have writen:
Dim row As New DataGridViewRow
row = Me.ImagesDataGridView.Rows(0)
Dim Temp As String = row.Cells("imagedata").Value.ToString
Dim ms As System.IO.MemoryStream
ms = New System.IO.MemoryStream(Temp)
PictureBox1.Image = New Bitmap(ms)
Table is in a datagridview. The fields 'image' contains the exact copy of an
'jpg' file entered in memo field in Access VBA with 'appendchunk').
Is it possible to create bitmap from the string ? How ?
What is row.PictureData ? Which kind of variable ?
How can I store memo data to can use your method ?
The example written by here above me does not function.
I'm a beginner, as you can guess.

Thanks for your attention, sincerely. I appreciate

Philip

"Martin Horn" <mv****@theinternet.com> a écrit dans le message de news:
qv****************@newsfe7-win.ntli.net...
If I understand you correctly you are trying to read an image from a
database, in which case you could try:

Dim ms as IO.MemoryStream
ms = New IO.MemoryStream(Row.PictureData)
PictureBox1.Image = New Bitmap(ms)

Hope this helps,

Martin

"philip" <ph**@philippe.com> wrote in message
news:43**********************@news.wanadoo.fr...
If I execute that :

Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
sw.Write(temp)
sw.Close()

the resulting file 'Text.txt' has the same length than the string 'temp'.
Same result with I Use BinayWriter.

BUT if 'temp' contains others characters than readable character, then
the result file has not the same length than the string.

So, if temp contains "ÿØÿá" (that is to say a string concanated with the
4 characters chr(255) & chr(216) & chr(255) and chr(225)), the file
writen is 8 octets long, and not 4 octets like the string (twice more in
the file than in the string). Same problem with BinaryWriter.

My problem is that I try to write, in a new 'jpg' file, some data loaded
in a memo file. This memo contains the exact image of a 'jpg' file which
I must reproduce.
So with the method exposed above, result cannot good, because this method
does'nt write exactly the octets.

In Access VBA, with the method "open for binary", all is good. I can
reconstruc the 'jpg' file from the memo, then I can see image in a
picturebox.

So I think that I don't use the good method in Visual Studio 2005 to copy
this string on a file without any change.

Can someone help me and tell me the methos to do that ?

Thanks for response.


Feb 3 '06 #3
Hi Phillip,

looking at my database, the pictures are stored as an OLE Object, which
seems to translate as a byte array.

this is how I retrieve a picture from my database, you would need to replace
"Imagedata" in the example with the name of the Column that contains your
picture data.

Dim row As New DataGridViewRow
row = Me.grid.Rows(0)
Dim ms As IO.MemoryStream
ms = New IO.MemoryStream(CType(row.Cells("Imagedata").Value , Byte()))
PictureBox1.Image = New Bitmap(ms)

To save an image use the function below to create a byte array and save that
to your database.

Private Function CreateByteImage() As Byte()
Dim ms As New IO.MemoryStream
Me.ComplexInscriptionPictureBox.Image.Save(ms, _
System.Drawing.Imaging.ImageFormat.jpg)

Dim arrImage() As Byte = ms.GetBuffer
ms.Close()

Return arrImage
End Function

I don't know if the above example will work with a memo field, but it might
be of some help.

Martin.
Feb 3 '06 #4
In fact, my image in memo field of Access Database is not at all a Ole
Object. (Ole increase dramtically the size of a database). So I imagined to
copy in memo fields the exact image of each 'jpg' files I want to save in
database. Each image must be less of 64000 octets obviously.
In Access VBA , to get image of 'jpg' file in field memo , I use :
Open "xxxx.jpg" for binary as #1
With 'get', I obtain all octets of file, and I copy it simply in memo field
with the memo field.AppendChunk method.

I try your solution :
Dim row As New DataGridViewRow
row = Me.ImagesDataGridView.Rows(0)
Dim ms As IO.MemoryStream
ms = New IO.MemoryStream(CType(row.Cells("Imagedata").Value ,
Byte()))
PictureBox1.Image = New Bitmap(ms)
and I have the following error on the fourth line :
'Unable to cast object of type 'System.String' to type 'System.Byte[]'

Due probably by the fact that the memo field doesn't store an ole object.

The problem would be rather to transform a string in a bitmap, or anything
that PictureBox can use to show the image.
To try to have for a simple string the same method that
'System.Drawing.Image.FromFile(fullFilename)' permit directly with a file.
The solution would be not too complicated, because string contains exactly
the same octets than the 'jpg' file.
But I am beginner...

I am sure there is a solution. If you can give me, I should be the most
happy man in the world.
Many thanks if you can.


"Martin Horn" <mv****@theinternet.com> a écrit dans le message de news:
We*****************@newsfe4-win.ntli.net...
Hi Phillip,

looking at my database, the pictures are stored as an OLE Object, which
seems to translate as a byte array.

this is how I retrieve a picture from my database, you would need to
replace "Imagedata" in the example with the name of the Column that
contains your picture data.

Dim row As New DataGridViewRow
row = Me.grid.Rows(0)
Dim ms As IO.MemoryStream
ms = New IO.MemoryStream(CType(row.Cells("Imagedata").Value , Byte()))
PictureBox1.Image = New Bitmap(ms)

To save an image use the function below to create a byte array and save
that to your database.

Private Function CreateByteImage() As Byte()
Dim ms As New IO.MemoryStream
Me.ComplexInscriptionPictureBox.Image.Save(ms, _
System.Drawing.Imaging.ImageFormat.jpg)

Dim arrImage() As Byte = ms.GetBuffer
ms.Close()

Return arrImage
End Function

I don't know if the above example will work with a memo field, but it
might be of some help.

Martin.

Feb 4 '06 #5
Finally, with the help of Stan Smith, this routine functions :

Dim row As New DataGridViewRow
row = Me.ImagesDataGridView.Rows(0)
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

' 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()
Dim myFile As New FileInfo("C:\out.jpg")
MsgBox(myFile.Length)

I have just to discover better for loop 'For i ...' wich is not very
'elegant'.
If you have an idea...

Thanks for your help.
Feb 5 '06 #6
"philip" <ph**@philippe.com>
I have just to discover better for loop 'For i ...' wich is not very
'elegant'.
If you have an idea...

Thanks for your help.


Philip,

Try this:

Dim myASCII As New ASCIIEncoding

myByteArray = myASCII.GetBytes(myString)

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Feb 5 '06 #7
That not works.
I think that he replace by chr(63) all byte whose value is more then 127
Thanks

"Stan Smith" <ss****@adsprogramming.com> a écrit dans le message de news:
ue**************@TK2MSFTNGP11.phx.gbl...
"philip" <ph**@philippe.com>
I have just to discover better for loop 'For i ...' wich is not very
'elegant'.
If you have an idea...

Thanks for your help.


Philip,

Try this:

Dim myASCII As New ASCIIEncoding

myByteArray = myASCII.GetBytes(myString)

Stan

Stan Smith
ACT! Certified Consultant
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Feb 5 '06 #8
"philip" wrote
That not works.
I think that he replace by chr(63) all byte whose value is more then 127
Thanks


Philip,

I didn't test it with higher values. I don't know of any more "elegant" way
to do it than the one you are using. Sometimes the solution that works is
the "elegant" one.

Stan

--
Stan Smith
ACT! Certified Consultant
ADS Programming Services
Birmingham, AL
205-222-1661
ssmith_at_adsprogramming.com
Feb 6 '06 #9
Thank you for response.
I found a solution much quick solution with stringBuilder. More 'elegant'...

Thanks four your response and for spending time for me.

Philip

"Stan Smith" <ss****@adsprogramming.com> a écrit dans le message de news:
Oy**************@TK2MSFTNGP12.phx.gbl...
"philip" wrote
That not works.
I think that he replace by chr(63) all byte whose value is more then 127
Thanks


Philip,

I didn't test it with higher values. I don't know of any more "elegant"
way to do it than the one you are using. Sometimes the solution that
works is the "elegant" one.

Stan

--
Stan Smith
ACT! Certified Consultant
ADS Programming Services
Birmingham, AL
205-222-1661
ssmith_at_adsprogramming.com

Feb 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: taccea | last post by:
I need to print a report, but I need to first draw lines and boxes for the data. I need to do it at print time using VBA. How do I do this? Any suggestions appreciated. Taccea
2
by: Daniel Goldman | last post by:
Hi, Any advice about both a BinaryReader and BinaryWriter containing same FileStream at same time? Like: Stream fs = new FileStream("output.dbf", FileMode.Create); BinaryReader br = new...
3
by: Peyman | last post by:
Hello all, This is my first c# program. And I seem to have a problem. Here is the code: FileStream myStream = new FileStream("d:\\test.txt", FileMode.Create, FileAccess.Write); BinaryWriter...
17
by: Filip Strugar | last post by:
Considering that the BinaryWriter/BinaryReader object closes the underlaying stream upon being gc collected, is the following code correct, and if it is what is the reason preventing BinaryWriter...
9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
8
by: Kai Bohli | last post by:
Hi all ! I've come across a huge problem (for me at least). I'm trying to send some initial graphics to a labelprinter. To do this, I load the graphics from resource and send it directly to...
4
by: Paul Steele | last post by:
I am writing a client/server application that communicates over tcp. The code I use to initiate the connection is as follows: NetworkStream networkStream = new NetworkStream(ClientSocket);...
7
by: Selden McCabe | last post by:
I'm using the following code to write some text to a file: objWriter = New StreamWriter(FullPath, True, System.Text.Encoding.ASCII) For nRow = 1 To sData.Length - 1...
3
by: philip | last post by:
If I execute that : Dim Temp as string = "This is a text" Dim sw As StreamWriter Dim fullFileName as string = "c:\text.txt" sw = New StreamWriter(fullFilename) sw.Write(temp) sw.Close() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.