saving an image from a byte array

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

    #1

    saving an image from a byte array

    I am saving image files from my website using the downloaddata on the web
    client. This gives me a byte array, which I am then saving to a file with
    the following code:

    ' Create the new, empty data file.
    If File.Exists(Img Dest) Then
    Console.WriteLi ne("{0} already exists!", ImgDest)
    Return
    End If
    Dim fs As New FileStream(ImgD est, FileMode.Create New)
    ' Create the writer for data.
    Dim w As New BinaryWriter(fs )
    ' Write data to Test.data.
    w.Write(ImgArra y)
    w.Close()
    fs.Close()

    This works, in that I end up with the file(s) on my machine, but if I try
    and open them they come up as invalid. Any clues? Am I missing a step to do
    with image headers or something?
    James.


  • Cor Ligthert [MVP]

    #2
    Re: saving an image from a byte array

    James,

    Have a look at this sample on our website.



    I hope this helps,

    Cor


    Comment

    • Mythran

      #3
      Re: saving an image from a byte array


      "JamesB" <jamesb@jmb.i s-a-geekWhoDoesntLi keSpam.com> wrote in message
      news:42ef3da0$0 $24548$da0feed9 @news.zen.co.uk ...[color=blue]
      >I am saving image files from my website using the downloaddata on the web
      >client. This gives me a byte array, which I am then saving to a file with
      >the following code:
      >
      > ' Create the new, empty data file.
      > If File.Exists(Img Dest) Then
      > Console.WriteLi ne("{0} already exists!", ImgDest)
      > Return
      > End If
      > Dim fs As New FileStream(ImgD est, FileMode.Create New)
      > ' Create the writer for data.
      > Dim w As New BinaryWriter(fs )
      > ' Write data to Test.data.
      > w.Write(ImgArra y)
      > w.Close()
      > fs.Close()
      >
      > This works, in that I end up with the file(s) on my machine, but if I try
      > and open them they come up as invalid. Any clues? Am I missing a step to
      > do with image headers or something?
      > James.
      >[/color]

      Since it's binary data you are writing, I would use the BinaryWriter. But,
      since it's an image, I would actually read the byte array into a
      MemoryStream, create an Image class instance, then call it's "Save" method.

      Mythran

      Comment

      • JamesB

        #4
        Re: saving an image from a byte array


        "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
        news:ebpoqh3lFH A.420@TK2MSFTNG P09.phx.gbl...[color=blue]
        >
        >
        > Since it's binary data you are writing, I would use the BinaryWriter.[/color]
        But,[color=blue]
        > since it's an image, I would actually read the byte array into a
        > MemoryStream, create an Image class instance, then call it's "Save"[/color]
        method.[color=blue]
        >[/color]


        Thanks for both replies- turns out after more checking my method did
        actually work... there was a bug in the way I was building my url from other
        places in the app that I hadn't spotted!
        I'll do some more testing and if it does give more problems I'll look into
        the suggestions you've both given.
        Thanks
        James


        Comment

        Working...