Home All Groups Group Topic Archive Search About

StreamWriter and BinaryWriter (same problem better explained)

Author
3 Feb 2006 4:12 PM
philip
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.

Author
3 Feb 2006 4:27 PM
Martin Horn
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

Show quoteHide quote
"philip" <p***@philippe.com> wrote in message
news:43e380fa$0$6685$8fcfb975@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.
>
>
>
Author
3 Feb 2006 4:58 PM
philip
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" <mvh***@theinternet.com> a écrit dans le message de news:
qvLEf.6227$K42.***@newsfe7-win.ntli.net...
Show quoteHide quote
> 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" <p***@philippe.com> wrote in message
> news:43e380fa$0$6685$8fcfb975@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.
>>
>>
>>
>
>
Author
3 Feb 2006 6:25 PM
Martin Horn
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.
Author
4 Feb 2006 2:00 AM
philip
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" <mvh***@theinternet.com> a écrit dans le message de news:
WeNEf.5459$Fy4.3***@newsfe4-win.ntli.net...
Show quoteHide quote
> 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.
>
Author
5 Feb 2006 3:32 PM
philip
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.
Author
5 Feb 2006 9:40 PM
Stan Smith
"philip" <p***@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
Author
5 Feb 2006 11:08 PM
philip
That not works.
I think that he replace by chr(63) all byte whose value is more then 127
Thanks

"Stan Smith" <ssm***@adsprogramming.com> a écrit dans le message de news:
uepu1ypKGHA.1***@TK2MSFTNGP11.phx.gbl...
Show quoteHide quote
> "philip" <p***@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
>
>
>
Author
6 Feb 2006 12:35 AM
Stan Smith
"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
Author
6 Feb 2006 1:38 PM
philip
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" <ssm***@adsprogramming.com> a écrit dans le message de news:
Oy5sLVrKGHA.1***@TK2MSFTNGP12.phx.gbl...
Show quoteHide quote
> "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
>
>