put bytes array in bmpdata, how?

J

Jeroen Ceuppens

I created new bitmap file, bitmapdata, now a want the pixeldata that are in
the array bytes[] put into the bmpData, what should I change in the code
unsafe? there is something wrong with p[0] = (byte)(bytes[0]); is makes all
the pixel in some color..........



// this code will be replace by a COM who gets a byte array

Stream stream = File.Open(@"d:\ImageWeftIllum.bmp", FileMode.Open);

bytes=new byte[stream.Length];

stream.Read(bytes,0,(int)stream.Length);

mem= new MemoryStream(bytes);

//

Bitmap bmp = new Bitmap(640,480);

BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width,
bmp.Height),

ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmpData.Stride;

System.IntPtr Scan0 = bmpData.Scan0;

unsafe

{

byte * p = (byte *)(void *)Scan0;

int nOffset = stride - bmp.Width*3;

int nWidth = bmp.Width * 3;

for(int y=0;y < bmp.Height;++y)

{

for(int x=0; x < nWidth; ++x )

{

p[0] = (byte)(bytes[0]);

++p;

}

p += nOffset;


}

}

bmp.UnlockBits(bmpData);

pictureBox1.Image=bmp;
 
W

Wiktor Zychla

I created new bitmap file, bitmapdata, now a want the pixeldata that are
in
the array bytes[] put into the bmpData, what should I change in the code
unsafe? there is something wrong with p[0] = (byte)(bytes[0]); is makes all
the pixel in some color..........

you can use Marshal.Copy instead of unsafe code. it should work (it works
for me in a similar scenario).

Marshal.Copy ( bytes, 0, bData.Scan0, size );

Regards,
Wiktor Zychla
 
J

Jeroen Ceuppens

idd the p[0] = (byte)(bytes[0]); is very wrong, the 0 should be someting
else like int i..........
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top