StreamWriter and BinaryWriter (same problem better explained)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • philip

    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.tx t"
    sw = New StreamWriter(fu llFilename)
    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.



  • Martin Horn

    #2
    Re: StreamWriter and BinaryWriter (same problem better explained)

    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.PictureDat a)
    PictureBox1.Ima ge = New Bitmap(ms)

    Hope this helps,

    Martin

    "philip" <phil@philippe. com> wrote in message
    news:43e380fa$0 $6685$8fcfb975@ news.wanadoo.fr ...[color=blue]
    > If I execute that :
    >
    > Dim Temp as string = "This is a text"
    > Dim sw As StreamWriter
    > Dim fullFileName as string = "c:\text.tx t"
    > sw = New StreamWriter(fu llFilename)
    > 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.
    >
    >
    >[/color]


    Comment

    • philip

      #3
      Re: StreamWriter and BinaryWriter (same problem better explained)

      From your message, I have writen:
      Dim row As New DataGridViewRow
      row = Me.ImagesDataGr idView.Rows(0)
      Dim Temp As String = row.Cells("imag edata").Value.T oString
      Dim ms As System.IO.Memor yStream
      ms = New System.IO.Memor yStream(Temp)
      PictureBox1.Ima ge = 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" <mvhorn@theinte rnet.com> a écrit dans le message de news:
      qvLEf.6227$K42. 233@newsfe7-win.ntli.net...[color=blue]
      > 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.PictureDat a)
      > PictureBox1.Ima ge = New Bitmap(ms)
      >
      > Hope this helps,
      >
      > Martin
      >
      > "philip" <phil@philippe. com> wrote in message
      > news:43e380fa$0 $6685$8fcfb975@ news.wanadoo.fr ...[color=green]
      >> If I execute that :
      >>
      >> Dim Temp as string = "This is a text"
      >> Dim sw As StreamWriter
      >> Dim fullFileName as string = "c:\text.tx t"
      >> sw = New StreamWriter(fu llFilename)
      >> 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.
      >>
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Martin Horn

        #4
        Re: StreamWriter and BinaryWriter (same problem better explained)

        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.Cell s("Imagedata"). Value, Byte()))
        PictureBox1.Ima ge = 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.ComplexInscr iptionPictureBo x.Image.Save(ms , _
        System.Drawing. Imaging.ImageFo rmat.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.


        Comment

        • philip

          #5
          Re: StreamWriter and BinaryWriter (same problem better explained)

          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.AppendChu nk method.

          I try your solution :
          Dim row As New DataGridViewRow
          row = Me.ImagesDataGr idView.Rows(0)
          Dim ms As IO.MemoryStream
          ms = New IO.MemoryStream (CType(row.Cell s("Imagedata"). Value,
          Byte()))
          PictureBox1.Ima ge = 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" <mvhorn@theinte rnet.com> a écrit dans le message de news:
          WeNEf.5459$Fy4. 3660@newsfe4-win.ntli.net...[color=blue]
          > 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.Cell s("Imagedata"). Value, Byte()))
          > PictureBox1.Ima ge = 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.ComplexInscr iptionPictureBo x.Image.Save(ms , _
          > System.Drawing. Imaging.ImageFo rmat.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.
          >[/color]


          Comment

          • philip

            #6
            Re: StreamWriter and BinaryWriter (same problem better explained)

            Finally, with the help of Stan Smith, this routine functions :

            Dim row As New DataGridViewRow
            row = Me.ImagesDataGr idView.Rows(0)
            Dim strImage As String = row.Cells("pict ure_field").Val ue.ToString

            Dim myByteArray(str Image.Length) As Byte
            Dim i As Integer
            For i = 0 To strImage.Length - 1
            myByteArray(i) = Asc(strImage.Ch ars(i))
            Next

            ' Convert the byte array to a MemoryStream
            Dim myMemoryStream As MemoryStream
            myMemoryStream = New MemoryStream(my ByteArray, 0,
            myByteArray.Len gth)
            myMemoryStream. Write(myByteArr ay, 0, myByteArray.Len gth)
            Debug.Print(myM emoryStream.Len gth)
            ' Display the image from the MemoryStream
            PictureBox1.Ima ge = Image.FromStrea m(myMemoryStrea m, True)

            ' Write the byte array to a new disk file
            Dim myBinaryWriter As New BinaryWriter(Fi le.OpenWrite("C :\out.jpg"))
            myBinaryWriter. Write(myByteArr ay)
            myBinaryWriter. Close()
            Dim myFile As New FileInfo("C:\ou t.jpg")
            MsgBox(myFile.L ength)

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

            Thanks for your help.


            Comment

            • Stan Smith

              #7
              Re: StreamWriter and BinaryWriter (same problem better explained)

              "philip" <phil@philippe. com>
              [color=blue]
              > I have just to discover better for loop 'For i ...' wich is not very
              > 'elegant'.
              > If you have an idea...
              >
              > Thanks for your help.
              >[/color]

              Philip,

              Try this:

              Dim myASCII As New ASCIIEncoding

              myByteArray = myASCII.GetByte s(myString)



              Stan

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

              ssmith_at_adspr ogramming.com



              Comment

              • philip

                #8
                Re: StreamWriter and BinaryWriter (same problem better explained)

                That not works.
                I think that he replace by chr(63) all byte whose value is more then 127
                Thanks

                "Stan Smith" <ssmith@adsprog ramming.com> a écrit dans le message de news:
                uepu1ypKGHA.119 2@TK2MSFTNGP11. phx.gbl...[color=blue]
                > "philip" <phil@philippe. com>
                >[color=green]
                >> I have just to discover better for loop 'For i ...' wich is not very
                >> 'elegant'.
                >> If you have an idea...
                >>
                >> Thanks for your help.
                >>[/color]
                >
                > Philip,
                >
                > Try this:
                >
                > Dim myASCII As New ASCIIEncoding
                >
                > myByteArray = myASCII.GetByte s(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_adspr ogramming.com
                >
                >
                >[/color]


                Comment

                • Stan Smith

                  #9
                  Re: StreamWriter and BinaryWriter (same problem better explained)

                  "philip" wrote
                  [color=blue]
                  > That not works.
                  > I think that he replace by chr(63) all byte whose value is more then 127
                  > Thanks
                  >[/color]

                  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_adspr ogramming.com


                  Comment

                  • philip

                    #10
                    Re: StreamWriter and BinaryWriter (same problem better explained)

                    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" <ssmith@adsprog ramming.com> a écrit dans le message de news:
                    Oy5sLVrKGHA.142 4@TK2MSFTNGP12. phx.gbl...[color=blue]
                    > "philip" wrote
                    >[color=green]
                    >> That not works.
                    >> I think that he replace by chr(63) all byte whose value is more then 127
                    >> Thanks
                    >>[/color]
                    >
                    > 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_adspr ogramming.com
                    >
                    >[/color]


                    Comment

                    Working...