操纵像素WritableBitmapEx

时间:2012-10-31 20:52:16

标签: c# wpf writablebitmap

在WritableBitmap中操作像素的快速方法是什么(我还使用WritableBitmapEx扩展)? SetPixel是一种非常慢的方法,比如填充类似Paint的应用程序的背景,还会做一些奇怪的事情,比如内存损坏(不知道为什么)。

1 个答案:

答案 0 :(得分:1)

SetPixel非常慢 - 它的真实性。 您应该使用LockBits方法,然后使用不安全的代码(像素指针)迭代所有像素。

样品:

// lock the bitmap.
var data = image.LockBits(
              new Rectangle(0, 0, image.Width, image.Height), 
              ImageLockMode.ReadWrite, image.PixelFormat);
try
{
    unsafe
    {
        // get a pointer to the data.
        byte* ptr = (byte*)data.Scan0;

        // loop over all the data.
        for (int i = 0; i < data.Height; i++)
        {
            for (int j = 0; j < data.Width; j++)
            {
                operate with pixels.
            }
        }
    }
}
finally
{
    // unlock the bits when done or when 
    // an exception has been thrown.
    image.UnlockBits(data);
}

我建议你阅读article

相关问题