速度 - 复制位图数据到数组或工作,它直接? [英] Speed - Copy bitmap data into array or work with it directly?

查看:114
本文介绍了速度 - 复制位图数据到数组或工作,它直接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个 LockedBitmap 类,简化了位图数据的工作在C#。目前,它是将数据复制到本地字节[] 数组,然后通过它的类的方法来获取/设置像素的颜色值的访问。

这是更快,更好比通过指针直接访问锁定的位图数据?是一个副本需要在所有?

编辑:我不要求如果可以直接用位图数据的工作,我的工作每天都这样用。我只是问了2种方法之间如果有必要在所有复制像素数据进行比较。


复制像素数据到一个临时数组:

  //创建的字节数组复制像素值
INT步=深度/ 8;
像素=新的字节[PixelCount *步骤]
IPTR = bitmapData.Scan0;

//将数据从指针数组
Marshal.Copy(IPTR,像素,0,Pixels.Length);
 

直接读取像素值:

 字节* P =(BYTE *)(无效*)bmData.Scan0.ToPointer();
INT WS = bmData.Stride;
字节*行=安培; P [I * WS]。
字节Gcolor =行[J]。
字节Bcolor =行[J + 1];
字节Rcolor =行[J + 2];
 

解决方案
  

这是更快,更好比访问锁定的位图数据   直接通过指针?

没有。它需要一个附加的拷贝操作,然后再操作到处理后的值复制回位图。

  

时需要在所有的副本吗?

只有在一个环境中不安全 code是不需要的或不可用。

I'm using a LockedBitmap class that simplifies working with bitmap data in C#. Currently it is copying the data into a local byte[] array which is then accessed by its class methods to get/set pixel color values.

Is this faster or better than accessing the locked bitmap data directly via the pointer? Is a copy needed at all?

Edit: I'm not asking if its possible to directly work with bitmap data, I work with this everyday. I'm just asking for a comparison between the 2 methods and if its necessary at all to copy pixel data.


Copying pixel data into a temp array:

// create byte array to copy pixel values
int step = Depth / 8;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0;

// Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);

Directly reading pixel values:

byte* p = (byte*)(void*)bmData.Scan0.ToPointer();
int ws = bmData.Stride;
byte* row = &p[i * ws];
byte Gcolor = row[j];
byte Bcolor = row[j + 1];
byte Rcolor = row[j + 2];

解决方案

Is this faster or better than accessing the locked bitmap data directly via the pointer?

No. It requires an additional copy operation, and then another operation to copy the processed values back to the bitmap.

Is a copy needed at all?

Only in an environment where unsafe code is undesired or unavailable.

这篇关于速度 - 复制位图数据到数组或工作,它直接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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