ke*****@gmail.com wrote:
Hi,
I have an app (written in .NET 2.0) which updates a picturebox
according to some user input (a slider control). when the user makes a
change i loop through all of the pixels, do a calculation and update
the picture. i currently use the GDI SetPixel method on each pixel of
the Pictureboxes image. This is proving far to slow, about 1.5 seconds
on average. This app needs to display the update as fast as possible.
Has anyone got any ideas as to how to speed this up? i was thinking
about trying to access the picturebox's memory space directly, pseudo
code:
For Each pixel in <PictureBox.Image's bytearray>
<PictureBox.Image's bytearray(pixel) = <new pixel value from
calculation>
Next
This way you're only updating some byte values in memory rather than
calling GDI method's.
Am i barking up the wrong tree? Has anyone got any better solutions?
Any help would be much appreciated.
Elsethread what you have written suggests that the only way to do what
you want to do is indeed to perform a calculation on each pixel's color
value in turn. So given that, I present this:
(from the docs for Bitmap.LockBits)
Private Sub LockUnlockBitsExample(ByVal e As PaintEventArgs)
' Create a new bitmap.
Dim bmp As New Bitmap("c:\fakePhoto.jpg")
' Lock the bitmap's bits.
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
' Get the address of the first line.
Dim ptr As IntPtr = bmpData.Scan0
' Declare an array to hold the bytes of the bitmap.
' This code is specific to a bitmap with 24 bits per pixels.
Dim bytes As Integer = bmp.Width * bmp.Height * 3
Dim rgbValues(bytes - 1) As Byte
' Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
' Set every red value to 255.
For counter As Integer = 0 To rgbValues.Length - 1 Step 3
rgbValues(counter) = 255
Next
' Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbVal ues, 0, ptr, bytes)
' Unlock the bits.
bmp.UnlockBits(bmpData)
' Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150)
End Sub
Things to note:
- the PixelFormat of the bitmap must be one with a definite
bits-per-pixel: you can't use indexed bitmaps like this
- the number of bits needed for each pixel depends on the PixelFormat of
the bitmap; as a consequence, the size of the Byte array needed also
depends on that
- the format of the data in the Byte array you get *also* depends on the
BPP of the PixelFormat. eg if it is 32 bpp, then you will get <alpha
byte<red byte<green byte<blue byterepeated; if it is 24bpp with
no alpha, you will get <red byte<green byte<blue byterepeated; and
all the others.
Finally I must point out that you shouldn't get too attached to this
technique - it's only appropriate when you (as in this case) *have* to
examine each and every pixel individually. Things other people have
mentioned, like ColorMatrix, are more appropriate for general image
manipulation.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version