Processing large bitmap images in WPF
<p>I need to process (change brightness, contrast etc) very large high-quality bitmaps (often over 10MPx) several times per second and need to update it on screen every time ( on Image control in WPF). Currently I'm using AForge.NET library for unmanaged image processing, but there are some problems I cannot solve. First of all, one operation takes ~300ms (without updating the screen) which is not acceptable for me. Here's sample code:</p>

<pre><code>UnmanagedImage _img;
BrightnessCorrection _brightness = new BrightnessCorrection();

void Load()
{
   _img = UnmanagedImage.FromManagedImage((Bitmap)Bitmap.FromFile("image.jpg"));
}

void ChangeBrightness(int val) // this method is invoked by changing Slider value - several times per second
{
   _brightness.AdjustValue = val;
   _brightness.ApplyInPlace(_img); // it takes ~300ms for image 22MPx, no screen update - just change brightness "in background"
} 
</code></pre>

<p>I have no experience in image processing, but I think it cannot be much faster since it is very high resolution. Am I right?</p>

<p>Another problem - how to efficiently update the screen? At the moment I have the following (ofc very bad) solution:</p>

<pre><code>void ChangeBrightness(int val)
{
   _brightness.AdjustValue = val;
   _brightness.ApplyInPlace(_img);

    using (MemoryStream ms = new MemoryStream())
    {
        using (Bitmap b = _img.ToManagedImage())
        {
            b.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);

            var bmp = new BitmapImage();
            bmp.BeginInit();
            bmp.StreamSource = ms;
            bmp.CacheOption = BitmapCacheOption.OnLoad;
            bmp.EndInit();

            MyImageControl.Source = new WriteableBitmap(bmp); // !!!
        }
    }
}
</code></pre>

<p>As you can see, every time new WriteableBitmap is created (you can imagine what is happenin). Instead of these "usings" I tried that way:</p>

<pre><code>    WriteableBitmapSource.Lock(); // this object (of type WriteableBitmap) is just MVVM ViewModel's property which is binded to MyImageControl.Source
    WriteableBitmapSource.Source.WritePixels(new Int32Rect(0, 0, _img.Width, _img.Height), _img.ImageData, _img.Stride * _img.Height * 3, _img.Stride, 0, 0); // image's PixelFormat is 24bppRgb
</code></pre>

<p>... but WritePixels method throws "Value does not fall within the expected range."  Any ideas why?
Any help will be much appreciated :)</p>

<p>P.S.
Is AForge.NET a good choice at all? Maybe there is better image processing lib?</p>

<p>sorry for my english ;P</p>
