By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

StreamWriter and BinaryWriter (same problem better explained)

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.