Unmanaged pixeldata into Picturebox

V

Vertilka

Hi friends,
I am getting a stream of PixelData (without header) from the network
(other computer).
I am trying to display this pixel data in a picture box with following
code (in "Form_Load"):

byte [] pixeldata = new byte[720*576*3]; // 720 width, 576 height, 3
channels(RGB)
GetPixelDataFromServer(ref pixeldata);
IntPtr ptr = (IntPtr) BitConverter.ToInt32(pixeldata, 0);
Bitmap bmp = new Bitmap (720, 576, 720 * 3, PixelFormat.Format24bppRgb,
ptr);
pictureBox1.Image = bmp;

well, this goes fine (compilation and running) but after that, after i
am out of the method, i get an excption that says:

An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.

and all, the bitmap and the picturebox are valid (not null);

Call stack is:
[<Non-user Code>]
Test.NET.exe!Test.NET.Form1.Main() Line 89 C#


Thanks,
Vertilka
 
D

Dmytro Lapshyn [MVP]

Hi,

This part of your code is apparently wrong:
IntPtr ptr = (IntPtr) BitConverter.ToInt32(pixeldata, 0);

To my eye, it won't give you a pointer to the memory location containing
pixel data. How about:

IntPtr pixels = Marshal.AllocHGlobal(720*576*3);
try
{
GetPixelDataFromServer(pixels); // The method should "understand" that a
raw pointer has been passed.
Bitmap bmp = new Bitmap (720, 576, 720 * 3, PixelFormat.Format24bppRgb,
pixels);
pictureBox1.Image = bmp;
}
finally
{
// Be careful here. First of all, check whether the Bitmap class takes the
ownership of the memory pointed by
// "pixels". If so, you won't need this line. If it doesn't take the
ownership, you still should be careful as for when
// it is safe to release the memory.
Marshal.FreeHGlobal(pixels);
}

Vertilka said:
Hi friends,
I am getting a stream of PixelData (without header) from the network
(other computer).
I am trying to display this pixel data in a picture box with following
code (in "Form_Load"):

byte [] pixeldata = new byte[720*576*3]; // 720 width, 576 height, 3
channels(RGB)
GetPixelDataFromServer(ref pixeldata);
IntPtr ptr = (IntPtr) BitConverter.ToInt32(pixeldata, 0);
Bitmap bmp = new Bitmap (720, 576, 720 * 3, PixelFormat.Format24bppRgb,
ptr);
pictureBox1.Image = bmp;

well, this goes fine (compilation and running) but after that, after i
am out of the method, i get an excption that says:

An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll

Additional information: Object reference not set to an instance of an
object.

and all, the bitmap and the picturebox are valid (not null);

Call stack is:
[<Non-user Code>]
Test.NET.exe!Test.NET.Form1.Main() Line 89 C#


Thanks,
Vertilka
 
M

Markus

Hi,
byte [] pixeldata = new byte[720*576*3]; // 720 width, 576 height, 3
channels(RGB)
GetPixelDataFromServer(ref pixeldata);
IntPtr ptr = (IntPtr) BitConverter.ToInt32(pixeldata, 0);
Bitmap bmp = new Bitmap (720, 576, 720 * 3, PixelFormat.Format24bppRgb,
ptr);
pictureBox1.Image = bmp;

I have done something similar with a MemoryStream, maybe this will work
with your case, just be sure, that the stream has bytes, which can be
parsed by Bitmap.FromStream(..).

using (MemoryStream ms = new MemoryStream(pixeldata))
{
pictureBox1.Image = Bitmap.FromStream(ms);
}

hth
Markus
 
D

Dmytro Lapshyn [MVP]

I agree - this is an option, but the Bitmap class might expect the stream to
contain the header as well as the pixel data.
 
M

Markus

Dmytro said:
I agree - this is an option, but the Bitmap class might expect the
stream to contain the header as well as the pixel data.

fully agree, that's what I meant with:

" [...] just be sure, that the stream has bytes, which can be parsed by
Bitmap.FromStream(..)."

sorry for my bad expression ;-)

Markus

PS. I am using this procedure to pass jpegs over the network with
remoting-calls.
 

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