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
Dmytro Lapshyn [MVP] - 21 Mar 2006 12:13 GMT
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);
}
> Hi friends,
> I am getting a stream of PixelData (without header) from the network
[quoted text clipped - 27 lines]
> Thanks,
> Vertilka
Markus - 21 Mar 2006 12:21 GMT
Hi,
> byte [] pixeldata = new byte[720*576*3]; // 720 width, 576 height, 3
> channels(RGB)
[quoted text clipped - 3 lines]
> 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
Dmytro Lapshyn [MVP] - 21 Mar 2006 12:38 GMT
I agree - this is an option, but the Bitmap class might expect the stream to
contain the header as well as the pixel data.
Markus - 21 Mar 2006 13:23 GMT
> 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.