CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    4

    Please help - Convert Image file to bytes

    Hi guys,

    How do i go about converting images files like jpeg's in to bytes?

    Im doing the work in c#, the .net 2.0 platform, if that makes any difference...

    really need some help on this, thanks!

    Bobski

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto
    Posts
    805

    Re: Please help - Convert Image file to bytes

    in asp.net 1.1 i use HttpPostedFile and use the "InputStream.Read" method of it .

    something like this

    Code:
    HttpPostedFile theFile = this.myFile.PostedFile; 
    byte[] uploadData = new Byte[nFileLen]; //nFileLen is the size of the file in bytes
    theFile.InputStream.Read(uploadData,0,nFileLen);
    //the above line reads the posted file into a byte array called uploadData.
    then uploadData has all the bytes in an array, and you can flush it or write it to a new file.

    hth,
    mcm
    rate my posts!
    mcm

  3. #3
    Join Date
    Nov 2006
    Posts
    4

    Red face Re: Please help - Convert Image file to bytes

    Appreciate the response, but i dont think it helps tho,

    any other suggestions pls

  4. #4
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Please help - Convert Image file to bytes

    Quote Originally Posted by Bobski
    Hi guys,

    How do i go about converting images files like jpeg's in to bytes?

    Im doing the work in c#, the .net 2.0 platform, if that makes any difference...

    really need some help on this, thanks!

    Bobski
    Is the file on your computer? You could try the following:

    Code:
    foreach(Byte b in File.ReadAllBytes(@"c:\test\sunset.jpg"))
        Console.Write("{0} ", b.ToString("X2"));
    Jason Isom
    .NET 2.0 / VS2005 Developer

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Please help - Convert Image file to bytes

    Code:
    byte[] bytes = null;
    using( MemoryStream ms = new MemoryStream() ) {
        myBmp.Save(ms, ... );
        ms.Seek(0,0);
        bytes = ms.ToArray();
    }
    // bytes contains the image

  6. #6
    Join Date
    Nov 2006
    Posts
    4

    Thumbs up Re: Please help - Convert Image file to bytes

    ***********************
    Is the file on your computer? You could try the following:


    Code:

    foreach(Byte b in File.ReadAllBytes(@"c:\test\sunset.jpg"))
    Console.Write("{0} ", b.ToString("X2"));
    *************************

    Yeh mate this seems to work, thx.

    Im basically trying to change it into bytes so i can send it over a bluetooth connection using OBEX to a mobile device. All i need to know now is how to convert this back to an image so i can display it on the phone. Oh how i love programming ;-)


    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured