我有一个方法需要尽可能快,它使用不安全的内存指针,它是我第一次进入这种类型的编码,所以我知道它可能会更快。LockBits性能关键代码
/// <summary>
/// Copies bitmapdata from one bitmap to another at a specified point on the output bitmapdata
/// </summary>
/// <param name="sourcebtmpdata">The sourcebitmap must be smaller that the destbitmap</param>
/// <param name="destbtmpdata"></param>
/// <param name="point">The point on the destination bitmap to draw at</param>
private static unsafe void CopyBitmapToDest(BitmapData sourcebtmpdata, BitmapData destbtmpdata, Point point)
{
// calculate total number of rows to draw.
var totalRow = Math.Min(
destbtmpdata.Height - point.Y,
sourcebtmpdata.Height);
//loop through each row on the source bitmap and get mem pointers
//to the source bitmap and dest bitmap
for (int i = 0; i < totalRow; i++)
{
int destRow = point.Y + i;
//get the pointer to the start of the current pixel "row" on the output image
byte* destRowPtr = (byte*)destbtmpdata.Scan0 + (destRow * destbtmpdata.Stride);
//get the pointer to the start of the FIRST pixel row on the source image
byte* srcRowPtr = (byte*)sourcebtmpdata.Scan0 + (i * sourcebtmpdata.Stride);
int pointX = point.X;
//the rowSize is pre-computed before the loop to improve performance
int rowSize = Math.Min(destbtmpdata.Width - pointX, sourcebtmpdata.Width);
//for each row each set each pixel
for (int j = 0; j < rowSize; j++)
{
int firstBlueByte = ((pointX + j)*3);
int srcByte = j *3;
destRowPtr[(firstBlueByte)] = srcRowPtr[srcByte];
destRowPtr[(firstBlueByte) + 1] = srcRowPtr[srcByte + 1];
destRowPtr[(firstBlueByte) + 2] = srcRowPtr[srcByte + 2];
}
}
}
那么有什么办法可以让这个更快吗?现在忽略了待办事项,稍后我有一些基线性能测量时就会遇到问题。
更新:对不起,应该提到,我使用这个而不是Graphics.DrawImage的原因是因为即时实现多线程,因为我不能使用DrawImage。
更新2:我仍然不满意的表现,我敢肯定还有几个ms可以有。
你为什么要调用LockBits?你是否在你发布的代码中没有的位图上直接做了些什么? becouse而不是锁定位图和复制每个字节的字节,你可以调用Graphics.DrawImage – 2009-04-11 18:23:38
我已经添加了另一个答案...看看是否有帮助:-) – chakrit 2009-04-11 19:11:06