Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2006

Tip: Looking for answers? Try searching our database.

Unmanaged pixeldata into Picturebox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vertilka - 20 Mar 2006 09:28 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.