Jon Slaughter said:
Hi John,
What performance issues are you having with LockBits? If you're
programming C# then you should surely be using unsafe code and not the
Marshal class for access.
Did you look at my code? I'm not sure what you mean by unsafe and
marshaling? I used unsafe to access the lockbits... its all in the code
I pasted...
crap... lol, I guess I didn't paste any code ;/
Ok, here it is... Its kinda messy but shows what I'm doing when using
lockbits(I pretty much copied and pasted from some other site that said
to use this method.
When I run this under VS I get about 1fps. When I run it outside of VS I
get about 10-20fps or so. (it might actually be fast but its slightly
jerky)
Thanks,
Jon
bmd = bitmap.LockBits(R, System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat);
ReadOnly doesn't look right for this.
unsafe
{
for (int y = 0; y < bmd.Height; y++)
{
byte* row = (byte*)bmd.Scan0 + (y * bmd.Stride);
for (int x = 0; x < bmd.Width; x++)
{
c = (int)(t) % 255;
row[x * PixelSize + 2] = (byte)(255*(Math.Pow((c/255F),0.8)));
row[x * PixelSize + 1] = (byte)(255*(Math.Pow((c/255F),2)));
row[x * PixelSize + 0] = (byte)(255*(Math.Pow((c/255F),3)));
}
}
} // unsafe
bitmap.UnlockBits(bmd);
BitmapGraphics2.DrawImage(bitmap, 0, 0);
Which part is the slowdown (use Diagnostics.Stopwatch)? The LockBits,
filling in the array, UnlockBits, or DrawImage?
I doubt the computations are the bottleneck, but you can surely remove
some redundant code there, use repeated addition to find row instead of
multiplying by y, precompute x*PixelSize, and (c / 255). Also
multiplying will be much faster than Math.Pow for the integral cases.
If I'm right and the for loops are fast enough, then you can surely get a
very good frame rate (i.e. hit monitor's refresh rate with plenty of
cycles to spare) using OpenGL with glDrawPixels. A little more work for
you, but you'll get direct transfers to video memory and thence to the
screen. If you move up to a texture with glTexImage2D, a lot of effects
become essentially free at that point as well (transparency for
watermarking or fading in and out, rotation, stretching, etc).