VB.NET Reading a Raw Image File into a PictureBox

393 views
Skip to first unread message

MegaDeth

unread,
Nov 3, 2006, 5:24:16 PM11/3/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I have a raw image (256x256 16bit unsigned) in a file (image.raw). I
would like to read this in my VB.NET program and create a
System.Drawing.Image from it. I am lost in all the available classes
and am not sure how to do it.

I have the part to read in the file into an Integer array, although I
am not sure if this is the best way.
Dim binReader As New
IO.BinaryReader(System.IO.File.OpenRead(fileName & ".raw"))
Dim intImage(256, 256) As Integer
Dim x, y As Integer
For x = 0 To 255
For y = 0 To 255
intImage(x, y) = binReader.ReadUInt16()
Next
Next

How do I convert this array to a System.Drawing.Image or
System.Drawing.Bitmap so that I can display the image in a PictureBox?
I have a feeling I have to use a MemoryStream but am unclear about
using it.

Thanks.

mostly_magic

unread,
Nov 6, 2006, 12:14:04 AM11/6/06
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
There are two methods of doing this: one using managed code, the other
using unsafe code which therefore can't be achieved using VB.NET.

Given the size of the image you're using, there's no need to use unsafe
code.

First off, you'll need to instantiate a new Bitmap image, which has the
same dimensions and pixel-depth as the source image. In this case, I'm
assuming that your image has no alpha channel, I will use a 48bpp
image:

Dim bmp As Bitmap = New Bitmap(256, 256, PixelFormat.Format48bppRgb)

Then you copy the color data for each pixel into the image using
SetPixel:

For y As Integer = 0 To 255
For x As Integer = 0 To 255
bmp.SetPixel(x, y, Color.FromArgb(...))
Next
Next

Cheers,
Mitchell Cowie

Reply all
Reply to author
Forward
0 new messages