Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You really need to use <a href="http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx" rel="nofollow"><code>LockBits</code></a> instead of <code>GetPixel</code> and <code>SetPixel</code>. </p> <p>So you create a <code>BitmapData</code> object that contains all of the pixel data:</p> <pre><code>// lock the input bitmap's bits System.Drawing.Imaging.BitmapData bmpData = original.LockBits(new Rectangle(0, 0, original.Width, original.Height), System.Drawing.Imaging.ImageLockMode.Read, original.PixelFormat); </code></pre> <p>Then, you can get the address of the first scan line (i.e. the first row of pixels):</p> <pre><code>IntPtr ptr = bmpData.Scan0; </code></pre> <p>Now you have two choices. If you're happy to mark your function as <code>unsafe</code> then you can access the pixels directly using pointer arithmetic, like this</p> <pre><code>byte* pPixels = (byte*)ptr.ToPointer(); </code></pre> <p>which gives you a pointer to the first byte of an RGB pixel (assuming 24bpp). Then you can access an individual pixel at <code>(x,y)</code> using pointer arithmetic. First you determine how many bytes per pixel (if you don't already know)</p> <pre><code>int nBytesPerPixel = Image.GetPixelFormatSize(original.PixelFormat) / 8; </code></pre> <p>Then calculate the index to the pixel you want</p> <pre><code>byte* pPixelAtXY = pPixels + (y * bmpData.Stride) + (x * nBytesPerPixel); </code></pre> <p>This gives you <code>unsafe</code> access to the pixels in a <code>Bitmap</code> which you can do for the input and output bitmaps for fastest speed. Note that to use unsafe code, you need to <a href="http://msdn.microsoft.com/en-us/library/chfa2zb8%28v=vs.100%29.aspx" rel="nofollow">mark your function as unsafe</a> and <a href="http://msdn.microsoft.com/en-us/library/ct597kb0%28v=vs.100%29.aspx" rel="nofollow">edit your project properties</a>.</p> <p>If you don't want to use <code>unsafe</code> code, you can still speed things up by copying all of the pixel data to a <code>byte</code> array before processing and then copy it back afterwards. As the MSDN example showed</p> <pre><code>// Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // Set every third value to 255. A 24bpp bitmap will look red. for (int counter = 2; counter &lt; rgbValues.Length; counter += 3) rgbValues[counter] = 255; // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); </code></pre> <p>Whichever method you use, when you have finished with the pixel data, you <em>must</em> release it using <code>UnlockBits</code></p> <pre><code>original.UnlockBits(bmpData); </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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