我需要每秒多次处理(更改亮度,对比度等)非常大的高质量位图(通常超过10MPx),并且每次都需要在屏幕上更新它(在WPF中的Image控件上)。目前我正在使用AForge.NET库进行非托管图像处理,但有一些问题我无法解决。首先,一个操作需要约300毫秒(不更新屏幕),这是我不能接受的。这里的示例代码:在WPF中处理较大的位图图像
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"
}
我没有经验的图像处理,但我认为它不能更快,因为它是非常高的分辨率。我对吗?
另一个问题 - 如何有效地更新屏幕?目前,我有以下的(OFC非常糟糕)解决方案:
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); // !!!
}
}
}
正如你可以看到,每一个新的WriteableBitmap的创建时间(你能想象是什么happenin)。相反,这些“usings”我试过这样:
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
...但WritePixels方法抛出“值没有在预期范围之内。”任何想法为什么? 任何帮助将不胜感激:)
P.S. AForge.NET是一个不错的选择吗?也许有更好的图像处理库?
对不起,我的英文; P
我想象中10MP是你的问题 - 任何机器都会受到影响。我安装了Photoshop,并且在6MP文件中需要一些时间来更改图像对比度/亮度。 – Charleh 2012-08-06 22:27:05