CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Byte to float

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Byte to float

    Hi there. I have got a question. Here goes the task. I have a byte array needs to be converted to bitmap. Here the code I use.

    public Bitmap return_bmp()
    {
    Bitmap bmp = new Bitmap(size_of_line, size_of_row,PixelFormat.Format24bppRgb);
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
    BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
    IntPtr ptr = bmpData.Scan0;

    byte[] rgbValues = new byte[bmpData.Stride*bmpData.Height];
    for (int i = 0; i < number_of_elements_in_array; i++)
    {
    rgbValues[i] = (byte)vals[i];
    rgbValues[2 * i] = (byte)vals[i];
    rgbValues[3 * i] = (byte)vals[i];
    }

    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bmpData.Stride*bmpData.Height);
    bmp.UnlockBits(bmpData);
    return bmp;
    }

    The problem is that I get the hodgepodge, not an image I crave for. The rgbValues structure is - the first size_of_line elements are the first line, the next value is the first pixel of the second line and so on. Please, help me. I've cudgeled my brain aboit it. Thanks in advance.
    Last edited by Avcrash; August 3rd, 2009 at 01:05 AM.

  2. #2
    Join Date
    Aug 2009
    Posts
    4

    Angry Re: Byte to float

    I 've mistaken having called this post so.

  3. #3
    Join Date
    Aug 2009
    Posts
    4

    Re: Byte to float

    Would anybody help, please?

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

    Re: Byte to float

    http://msdn.microsoft.com/en-us/libr...itmapdata.aspx

    check your loop. shouldn't it be i += 3, not i++

  5. #5
    Join Date
    Aug 2009
    Posts
    4

    Re: Byte to float

    Thanks for the link. I have seen it before. Every value in vals should represent a pixel in a resulting image, so I set every component of RGB to be vals[i]. Any other suggestions? i+=3 doesn't work either.
    Last edited by Avcrash; August 3rd, 2009 at 11:17 AM.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Byte to float

    Have you tried something simple like this?

    Code:
    ImageConverter ic = new ImageConverter();
    Image img = (Image)ic.ConvertFrom(byteArray);
    Bitmap bitmap1 = new Bitmap(img);
    Also, there is a Bitmap constructor which takes a Stream, so you could wrap the byte array in a stream and use that as well. Of course, this depends a bit on the structure of your data.

    Also, you do know that Windows stores rgb values in revers byte order, right? When you are working directly with an image buffer you need to know this.
    Last edited by BigEd781; August 3rd, 2009 at 02:06 PM.

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