如何转换为位图字节[,,]快? [英] How to convert Bitmap to byte[,,] faster?

查看:207
本文介绍了如何转换为位图字节[,,]快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的功能:

    public static byte[, ,] Bitmap2Byte(Bitmap image)
    {
        int h = image.Height;
        int w = image.Width;

        byte[, ,] result= new byte[w, h, 3];

        for (int i = 0; i < w; i++)
        {
            for (int j = 0; j < h; j++)
            {
                Color c= image.GetPixel(i, j);
                result[i, j, 0] = c.R;
                result[i, j, 1] = c.G;
                result[i, j, 2] = c.B;
            }
        }

        return result;
    }

但它需要近6秒转换1800x1800的图像。我可以做得更快?

But it takes almost 6 seconds to convert 1800x1800 image. Can I do this faster?

编辑:结果
好吧,我发现这一点:的http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx

有很好的例子。唯一的问题我已经是关于 Marshal.Copy 。我可以直接将数据复制到字节[,,]


OK, I found this: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx
There is nice example. Only question I have is about Marshal.Copy. Can I make it copy data directly into byte[,,] ?

编辑2:
OK,有时我得到的像素值奇怪,他们似乎并没有跟随R0 G0 B0 R1 G1 B1规则。为什么呢?没关系。想通了。

修改3:
做好了。 0,13s VS 5,35s:)

EDIT 3: Made it. 0,13s vs 5,35s :)

推荐答案

您可以大大通过使用从的BitmapData 对象加快这> Bitmap.LockBits 。谷歌C#位图LockBits为一堆例子。

You can speed this up considerably by using a BitmapData object which is returned from Bitmap.LockBits. Google "C# Bitmap LockBits" for a bunch of examples.

GetPixel 是痛苦的,痛苦的缓慢,使得它(讽刺)完全不适合单个像素的操作。

GetPixel is painfully, painfully slow, making it (ironically) completely unsuitable for the manipulation of individual pixels.

这篇关于如何转换为位图字节[,,]快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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