private static Bitmap RgbProcess(Bitmap destImg, int destX, int destY, int destWidth, int destHeight, Bitmap srcImg, int srcX, int srcY, RgbProcessDelegate rgbProcessFunction) { var dst = destImg; //new Bitmap(destImg); var dstData = dst.LockBits(new Rectangle(destX, destY, destWidth, destHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); var dstStride = Math.Abs(dstData.Stride); var dstScan0 = dstData.Scan0; var dstBuffer = new byte[dstStride]; var src = srcImg; //new Bitmap(srcImg); var srcData = src.LockBits(new Rectangle(srcX, srcY, destWidth, destHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); var srcStride = Math.Abs(srcData.Stride); var srcScan0 = srcData.Scan0; var srcBuffer = new byte[srcStride]; for (var y = 0; y < destHeight; y++) { System.Runtime.InteropServices.Marshal.Copy(srcScan0 + y * srcStride, srcBuffer, 0, srcStride); System.Runtime.InteropServices.Marshal.Copy(dstScan0 + y * dstStride, dstBuffer, 0, dstStride); for (var x = 0; x < destWidth; x += 3) { rgbProcessFunction(srcBuffer[x + 2], srcBuffer[x + 1], srcBuffer[x], ref dstBuffer[x + 2], ref dstBuffer[x + 1], ref dstBuffer[x]); } System.Runtime.InteropServices.Marshal.Copy(dstBuffer, 0, dstScan0 + y * dstStride, dstStride); } //unsafe //{ // byte* pDst = (byte*)(void*)dstScan0; // byte* pSrc = (byte*)(void*)srcScan0; // for (int y = 0; y < destHeight; y++) // { // for (int x = 0; x < destWidth; x++) // { // rgbProcessFunction( // pSrc[x * 3 + 2 + y * srcStride], pSrc[x * 3 + 1 + y * srcStride], pSrc[x * 3 + y * srcStride], // ref pDst[x * 3 + 2 + y * dstStride], ref pDst[x * 3 + 1 + y * dstStride], ref pDst[x * 3 + y * dstStride] // ); // } // } //} src.UnlockBits(srcData); dst.UnlockBits(dstData); return(dst); }
private static Bitmap RgbProcess(Bitmap destImg, int destX, int destY, int destWidth, int destHeight, Bitmap srcImg, int srcX, int srcY, RgbProcessDelegate rgbProcessFunction) { var dst = destImg; //new Bitmap(destImg); var dstData = dst.LockBits(new Rectangle(destX, destY, destWidth, destHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); var dstStride = Math.Abs(dstData.Stride); var dstScan0 = dstData.Scan0; var dstBuffer = new byte[dstStride]; var src = srcImg; //new Bitmap(srcImg); var srcData = src.LockBits(new Rectangle(srcX, srcY, destWidth, destHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); var srcStride = Math.Abs(srcData.Stride); var srcScan0 = srcData.Scan0; var srcBuffer = new byte[srcStride]; for (var y = 0; y < destHeight; y++) { System.Runtime.InteropServices.Marshal.Copy(srcScan0 + y * srcStride, srcBuffer, 0, srcStride); System.Runtime.InteropServices.Marshal.Copy(dstScan0 + y * dstStride, dstBuffer, 0, dstStride); for (var x = 0; x < destWidth; x += 3) { rgbProcessFunction(srcBuffer[x + 2], srcBuffer[x + 1], srcBuffer[x], ref dstBuffer[x + 2], ref dstBuffer[x + 1], ref dstBuffer[x]); } System.Runtime.InteropServices.Marshal.Copy(dstBuffer, 0, dstScan0 + y * dstStride, dstStride); } //unsafe //{ // byte* pDst = (byte*)(void*)dstScan0; // byte* pSrc = (byte*)(void*)srcScan0; // for (int y = 0; y < destHeight; y++) // { // for (int x = 0; x < destWidth; x++) // { // rgbProcessFunction( // pSrc[x * 3 + 2 + y * srcStride], pSrc[x * 3 + 1 + y * srcStride], pSrc[x * 3 + y * srcStride], // ref pDst[x * 3 + 2 + y * dstStride], ref pDst[x * 3 + 1 + y * dstStride], ref pDst[x * 3 + y * dstStride] // ); // } // } //} src.UnlockBits(srcData); dst.UnlockBits(dstData); return dst; }