Note that there are some explanatory texts on larger screens.

plurals
  1. POLockBits Performance Critical Code
    primarykey
    data
    text
    <p>I have a method which needs to be as fast as it possibly can, it uses unsafe memory pointers and its my first foray into this type of coding so I know it can probably be faster.</p> <pre><code> /// &lt;summary&gt; /// Copies bitmapdata from one bitmap to another at a specified point on the output bitmapdata /// &lt;/summary&gt; /// &lt;param name="sourcebtmpdata"&gt;The sourcebitmap must be smaller that the destbitmap&lt;/param&gt; /// &lt;param name="destbtmpdata"&gt;&lt;/param&gt; /// &lt;param name="point"&gt;The point on the destination bitmap to draw at&lt;/param&gt; 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 &lt; 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 &lt; 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]; } } } </code></pre> <p>So is there anything that can be done to make this faster? Ignore the todo for now, ill fix that later once I have some baseline performance measurements.</p> <p><strong>UPDATE:</strong> Sorry, should have mentioned that the reason i'm using this instead of Graphics.DrawImage is because im implementing multi-threading and because of that I cant use DrawImage.</p> <p><strong>UPDATE 2:</strong> I'm still not satisfied with the performance and i'm sure there's a few more ms that can be had.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload