473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastest way to update a Picturebox's image

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.

Alex

Jul 24 '06 #1
7 5983
On 24 Jul 2006 10:08:16 -0700, 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.

Alex

It would be helpful to know exactly what you are changing (i.e. Contrast, Saturation)?

Gene
Jul 24 '06 #2
Hi gene,

The calculation is a bespoke alteration that matches the 3rd party
software we're integrating with. in a nutshell for each RGB of every
pixel i have to to a look up based on the starting byte value and the
correction value. this look up gives a new byte value. so there are
three lookups for each pixel that returns a new RGB value. what i need
to know is the fasted possible way of setting this new pixel value to
the picturebox.

Hope that helps,
Alex

gene kelley wrote:
On 24 Jul 2006 10:08:16 -0700, 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.

Alex


It would be helpful to know exactly what you are changing (i.e. Contrast, Saturation)?

Gene
Jul 25 '06 #3
On 25 Jul 2006 02:38:58 -0700, ke*****@gmail.com wrote:
>Hi gene,

The calculation is a bespoke alteration that matches the 3rd party
software we're integrating with. in a nutshell for each RGB of every
pixel i have to to a look up based on the starting byte value and the
correction value. this look up gives a new byte value. so there are
three lookups for each pixel that returns a new RGB value. what i need
to know is the fasted possible way of setting this new pixel value to
the picturebox.

Hope that helps,
Alex

gene kelley wrote:
>On 24 Jul 2006 10:08:16 -0700, 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.

Alex


It would be helpful to know exactly what you are changing (i.e. Contrast, Saturation)?

Gene

You may want to investigate the ColorMatrix Class. If you are using VB2005, go to the help menu,
search GDI+ Image. Download the example. The example shows a couple of uses of the ColorMatrix
like convert to grayscale which would be fairly slow using SetPixel method.

Back in VB6, would have used Get/Set DibBits routines in place of Get/Set Pixels, but I have not
tried using any of those routines to day in .NET.

Gene

Jul 26 '06 #4
Hi

I haven't had to do any of this in .Net yet but you may want to look at
using a Bit Blit technique (which is what you're really describing when
you're talking about doing it in memory) - it's commonly referred to as
BitBlt and involves an API call.

Hope that helps
Martin
gene kelley wrote:
On 25 Jul 2006 02:38:58 -0700, ke*****@gmail.com wrote:
Hi gene,

The calculation is a bespoke alteration that matches the 3rd party
software we're integrating with. in a nutshell for each RGB of every
pixel i have to to a look up based on the starting byte value and the
correction value. this look up gives a new byte value. so there are
three lookups for each pixel that returns a new RGB value. what i need
to know is the fasted possible way of setting this new pixel value to
the picturebox.

Hope that helps,
Alex

gene kelley wrote:
On 24 Jul 2006 10:08:16 -0700, 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.

Alex
It would be helpful to know exactly what you are changing (i.e. Contrast, Saturation)?

Gene


You may want to investigate the ColorMatrix Class. If you are using VB2005, go to the help menu,
search GDI+ Image. Download the example. The example shows a couple of uses of the ColorMatrix
like convert to grayscale which would be fairly slow using SetPixel method.

Back in VB6, would have used Get/Set DibBits routines in place of Get/Set Pixels, but I have not
tried using any of those routines to day in .NET.

Gene
Jul 26 '06 #5
On 26 Jul 2006 03:00:58 -0700, "Pritcham" <do******************@hotmail.comwrote:
>Hi

I haven't had to do any of this in .Net yet but you may want to look at
using a Bit Blit technique (which is what you're really describing when
you're talking about doing it in memory) - it's commonly referred to as
BitBlt and involves an API call.

Hope that helps
Martin

BitBlt is simply a method of copying data form a source to a destination. It has no use with
regards to "changing" the color data in the source bitmap.

In .Net, the BitBlt API is wrapped in the Graphics.CopyFromScreen method.
Gene

Jul 26 '06 #6
Hi

I'm aware of that but the original post asked about whether it was
possible to do the work in memory as opposed to changing each pixel 1
at a time - if you're doing something like that then I understand that
it is (or can be) quicker to do in memory and then bitblt the results
back to the picture box data.
gene kelley wrote:
On 26 Jul 2006 03:00:58 -0700, "Pritcham" <do******************@hotmail.comwrote:
Hi

I haven't had to do any of this in .Net yet but you may want to look at
using a Bit Blit technique (which is what you're really describing when
you're talking about doing it in memory) - it's commonly referred to as
BitBlt and involves an API call.

Hope that helps
Martin


BitBlt is simply a method of copying data form a source to a destination. It has no use with
regards to "changing" the color data in the source bitmap.

In .Net, the BitBlt API is wrapped in the Graphics.CopyFromScreen method.
Gene
Jul 26 '06 #7
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
Jul 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3863
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
3
63294
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
4
1997
by: TomA | last post by:
Hi All, I have a picturebox on a form containing the photo of a person. As you advance through the records, the photo updates. Rather than storing the images in an inefficient blob field in a...
0
991
by: =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= | last post by:
I'm having problems on drawing an image inside a picturebox. The situation is this: I have a picturebox with an image.whenever i click the mouse right button, another picturebox will appear on top...
5
10286
by: GoGs | last post by:
How this vb6 code convert in c# dotnet2 ----- Image1.Visible = False Image1.Picture = LoadPicture("c:\image008.jpg") Dim x, y As Single x = Image1.Width y = Image1.Height Do While x...
3
22320
by: Andrzej | last post by:
I have a picturebox on my C# .NET form. The picturebox size mode is set to zoom. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
2
8573
by: =?Utf-8?B?Sm9uIFBhcnJ5?= | last post by:
Hi, I've got a Picturebox, into which I am putting a frame bitmap from a video camera. The camera bitmap is larger than the picturebox, which has SizeMode set to "Zoom" which causes the bitmap...
4
5539
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
5
5371
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.