C#LockBits性能比较(INT [,]为byte []) [英] C# LockBits perfomance (int[,] to byte[])

查看:1183
本文介绍了C#LockBits性能比较(INT [,]为byte [])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Graphics g;
using (var bmp = new Bitmap(_frame, _height, PixelFormat.Format24bppRgb))
{
    var data = bmp.LockBits(new Rectangle(0, 0, _frame, _height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    var bmpWidth = data.Stride;
    var bytes = bmpWidth * _height;
    var rgb = new byte[bytes];
    var ptr = data.Scan0;
    Marshal.Copy(ptr, rgb, 0, bytes);

    for (var i = 0; i < _frame; i++)
    {
        var i3 = (i << 1) + i;
        for (var j = 0; j < _height; j++)
        {
            var ij = j * bmpWidth + i3;
            var val = (byte)(_values[i, j]);
            rgb[ij] = val;
            rgb[ij + 1] = val;
            rgb[ij + 2] = val;
        }
    }

    Marshal.Copy(rgb, 0, ptr, bytes);
    bmp.UnlockBits(data);

    g = _box.CreateGraphics();
    g.InterpolationMode = InterpolationMode.NearestNeighbor;
    g.DrawImage(bmp, 0, 0, _box.Width, _box.Height);
}
g.Dispose();



我使用此代码转换的图片框RGB值(灰度)的数组,但它的速度慢。请告诉我,我的错误。
目前,441 000项的数组35毫秒处理。
我需要处理的400万同时数组。

I use this code to convert an array of RGB values ​​(grayscale) in the PictureBox, but it's slow. Please tell me my mistakes. At the moment, an array of 441 000 items handled for 35 ms. I need to handle an array of 4 million for the same time.

推荐答案

您可以跳过第一个 Array.Copy ,你从图像中的数据复制到阵列中,你会在阵列中进行反正覆盖所有的数据。

You can skip the first Array.Copy where you copy the data from the image to the array, as you will be overwriting all the data in the array anyway.

这将剃掉类似的25%的时间,但如果你想更快,你将不得不使用不安全的代码块,这样就可以使用指针。这样,你可以得到解决的范围检查,当你访问数组,你可以直接将数据写入到的图像数据而不是复制它。

That will shave off something like 25% of time, but if you want it faster you will have to use an unsafe code block so that you can use pointers. That way you can get around the range checking when you access arrays, and you can write the data directly into the image data instead of copying it.

这篇关于C#LockBits性能比较(INT [,]为byte [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆