How to get a picture into a byte array

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

    How to get a picture into a byte array

    Hi

    I'm trying to get a picture uploaded to the picture attribute in Active Directory. I've got most of it working, the only thing I need now, is to convert this picture to a byte array, so that I can save it in the AD

    Does anyone know how to do this

    Thank you VERY much in advance

    Rasmus
  • Cor

    #2
    Re: How to get a picture into a byte array

    Hi Rasmus,

    Try this

    Dim ms As New MemoryStream
    Dim arrImage() As Byte
    originalImage.S ave(ms, ImageFormat.Jpe g)
    arrImage = ms.GetBuffer

    I thought ImageFormat.Bmp is better but I needed this for something extra

    I hope this helps,

    Cor


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: How to get a picture into a byte array

      * "=?Utf-8?B?UmFzbXVzIFR lZ2xnYWFyZA==?= " <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
      > I'm trying to get a picture uploaded to the picture attribute in
      > Active Directory. I've got most of it working, the only thing I need
      > now, is to convert this picture to a byte array, so that I can save it
      > in the AD.[/color]

      I am not familiar with AD, but that do you want to say with "uploaded to
      the picture attribute"? In what format does the picture get uploaded?

      --
      Herfried K. Wagner [MVP]
      <http://www.mvps.org/dotnet>

      Comment

      • Rasmus Teglgaard

        #4
        Re: How to get a picture into a byte array

        Hi Cor

        Your my man! It worked - apparently

        The reason for the "apparently " is that, I get no errors (which is usually a good thing :0)
        The thing is, that I am not able see if this worked, before I can get the binary data back from the active directory and convert them back to an image and display it

        So, I was wondering if you (or others) could help me with this - to get the binary data back and convert them back to the picture again

        I can't express how much help you've been, thanks again

        Rasmu


        ----- Cor wrote: ----

        Hi Rasmus

        Try thi

        Dim ms As New MemoryStrea
        Dim arrImage() As Byt
        originalImage.S ave(ms, ImageFormat.Jpe g
        arrImage = ms.GetBuffe

        I thought ImageFormat.Bmp is better but I needed this for something extr

        I hope this helps

        Co



        Comment

        • Rasmus Teglgaard

          #5
          Re: How to get a picture into a byte array

          It should be uploaded in a byte array (binary).

          Rasmus

          ----- Herfried K. Wagner [MVP] wrote: -----

          * "=?Utf-8?B?UmFzbXVzIFR lZ2xnYWFyZA==?= " <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
          > I'm trying to get a picture uploaded to the picture attribute in
          > Active Directory. I've got most of it working, the only thing I need
          > now, is to convert this picture to a byte array, so that I can save it
          > in the AD.[/color]

          I am not familiar with AD, but that do you want to say with "uploaded to
          the picture attribute"? In what format does the picture get uploaded?

          --
          Herfried K. Wagner [MVP]
          <http://www.mvps.org/dotnet>

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: How to get a picture into a byte array

            * "=?Utf-8?B?UmFzbXVzIFR lZ2xnYWFyZA==?= " <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
            > It should be uploaded in a byte array (binary).[/color]

            \\\
            Dim fs As FileStream = New FileStream( _
            "C:\WINDOWS\Ang ler.bmp", _
            FileMode.Open _
            )
            Dim br As BinaryReader = New BinaryReader(fs )
            Dim abyt() As Byte = br.ReadBytes(fs .Length)
            br.Close()
            ///

            --
            Herfried K. Wagner [MVP]
            <http://www.mvps.org/dotnet>

            Comment

            • Rasmus Teglgaard

              #7
              Re: How to get a picture into a byte array

              Hi Herfried,

              Thank you very much for your apply.
              I've already gotten a solution from Cor though, and I've tried that, and it appears to be working. In this case, I'm using a memorystream rather than the filestream.

              I've got one last question though.
              I've now managed to get the picture into the active directory (I think). Now I need to get it back. So now it's the other way around.

              How do I make a byte array to a memorystream, that I can then import to a picturebox in VB?

              Thanks in advance,
              Rasmus

              ----- Herfried K. Wagner [MVP] wrote: -----

              * "=?Utf-8?B?UmFzbXVzIFR lZ2xnYWFyZA==?= " <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
              > It should be uploaded in a byte array (binary).[/color]

              \\ Dim fs As FileStream = New FileStream( _
              "C:\WINDOWS\Ang ler.bmp", _
              FileMode.Open _
              )
              Dim br As BinaryReader = New BinaryReader(fs )
              Dim abyt() As Byte = br.ReadBytes(fs .Length)
              br.Close()
              ///

              --
              Herfried K. Wagner [MVP]
              <http://www.mvps.org/dotnet>

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: How to get a picture into a byte array

                * "=?Utf-8?B?UmFzbXVzIFR lZ2xnYWFyZA==?= " <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
                > How do I make a byte array to a memorystream, that I can then import to a picturebox in VB?[/color]

                \\\
                Dim ms As New MemoryStream(ab yt)
                Dim img as Image = Image.FromStrea m(ms)
                ///

                Keep the 'MemoryStream' alive as long as you use the image.

                --
                Herfried K. Wagner [MVP]
                <http://www.mvps.org/dotnet>

                Comment

                • Rasmus Teglgaard

                  #9
                  Re: How to get a picture into a byte array

                  OK thanks - that helped a lot

                  Rasmus

                  Comment

                  Working...