Drawing a bitmap from an array - FAST

Joined
Mar 3, 2003
Messages
9
Location
Norway
I recive an array of bytes from a USB 2.0 device.
The array is a 800x600 grayscale picture.
The array is named Buffer
I use this code to present the image:

Private Sub BuildImage()
Dim Gray As Byte
Dim c, x, y As Integer
c=0
For y = 0 To 599
For x = 0 To 799
Gray = Buffer(c)
VideoBitmap.SetPixel(x, y, Color.FromArgb(Gray, Gray, Gray))
c = c + 1
Next
Next
End Sub

I takes about .5 sec to complete this on my PC. Wich gives me a frame rate of 2 pictures per sec.

Im looking for a faster way of displaying the picture.
Probably setpixel is not the fastest way?
As you see I have to make a RGB code for the grayscale Color.FromArgb(Gray, Gray, Gray).
Is there some way of inserting the grayscale values directly?

Thanx in advance
:D
 
You might want to check out the various constructors for the Bitmap class. One of them accepts a pointer to raw data as one of its parameters, and would certainly be the best way of doing what youre trying to do, if you can get it working.
 
i agree with divil on the solution.

all u need is a pointer to the array start. and then it goes smooth from there... with framerates about 10fps i think
 
OK guys!

Thanx allot for your answers, butI could not make it work.
So to test this I created a new form, slapped on a PictureBox and wrote this code:

Code:
    Dim Buffer As Byte()
    Dim VideoBitmap As Bitmap

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x, y, c As Integer
        ReDim Buffer(480000)
        c = 1
        For y = 1 To 600
            For x = 1 To 800
                Buffer(c) = CByte(255 / 800 * x)
                c = c + 1
            Next
        Next
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim BufferPointer As IntPtr
        Dim Stride As Integer
        BufferPointer = Nothing How do I get a pointer ro an Array?
        Stride = 800 * 8        What is Stride??
        VideoBitmap = New Bitmap(800, 600, Stride, Drawing.Imaging.PixelFormat.Format8bppIndexed, BufferPointer)
        VideoBitmap.MakeTransparent()
        PictureBox1.Refresh()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        e.Graphics.DrawImage(VideoBitmap, 0, 0, PictureBox.Width, PictureBox.Height)
    End Sub

Now Ive got a few moe questions;
How do I get a pointer to my Array (Buffer)?
What is Stride??
(The memory size of a row in the stream of pixel data???)
(I just took the number of pixels in a row and multiplied with eight since they are byte?? Probably wrong.)
Please take the time to answer my stupid questions
 
u could try this:

Code:
on System.Runtime.InteropServices.Marshal
[C#]
public static void Copy(
   byte[] source,
   int startIndex,
   IntPtr destination,
   int length
);

it copies an array of bytes to a pointer. dunno if its the best... but...
 
but its a 2 dim byte array

The Marshal.Copy deals with 1 dim byte array.
so how can you load a 2 Dim byte array into a pointer ?
 
hrm... the images Stride is the size of a line of the image in Bytes.
(800x600 image at 32bpp = stride of 800pixels * (32bits/8bitsperbyte))

hope this helps :)
 
Not sure if this would work but you could try it...

Code:
            Dim memory As New IO.MemoryStream(ByteArray)
            Dim bmp As New Bitmap(memory)
            memory.Close()
 
Back
Top