I'm not sure I would be as concerned about the CPU percentage used (which is
regulated by the Framework and the OS) as I would about the expense of using
GetPixel to read every pixel from a Bitmap. Here's a little technique that
will speed things up immensely, using unsafe code:
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;
for(int y=0;y<b.Height;++y)
{
for(int x=0; x < nWidth; ++x )
{
// do something with the 3 bytes of the pixel,
//.....
// then increment the pointer.
p +=3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
<ru****@toad.de> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi,
I developed a small app that uses the following code snippet:
for (x=0;x<clipBitmap.Width;x+=2)
{
for (y=0;y<clipBitmap.Height;y+=2)
{
/* get hue of current pixel (0-360) */
index=Convert.ToInt16(clipBitmap.GetPixel(x,y).Get Hue());
/* increment hue value */
Hue[index]++;
/* same with brightness */
index=Convert.ToInt16(clipBitmap.GetPixel(x,y).Get Brightness()*360.0);
Bri[index]++;
}
}
When the application reaches that code, the CPU goes up to 99% for the
time the code
needs to run. Is it possible to define a CPU limit or something else so
that the CPU does
not hit 99%?
Thanks in advance.
M.