I have a method that should be as fast as possible, it uses unsafe memory pointers and its first foray into this type of encoding, so I know that it can be faster.
/// <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]; } } }
So what can be done to make it faster? Ignore todo for now, itβs bad to fix it later, as soon as I have some baseline performance measurements.
UPDATE: Sorry, I should have mentioned that the reason I use this instead of Graphics.DrawImage is that im implements multithreading and because of this I cannot use DrawImage.
UPDATE 2: I'm still not happy with the performance, and I'm sure there are a few more ms that can be executed.
source share