Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What would be slow is the call to <code>GetPixel</code>. If you want to call it only as needed, you could use something like this:</p> <pre><code>open System.Drawing let lazyPixels (image:Bitmap) = let Width = image.Width let Height = image.Height let pixels : Lazy&lt;byte&gt;[,,] = Array3D.zeroCreate 3 Width Height for i = 0 to Width-1 do for j = 0 to Height-1 do let point = lazy image.GetPixel(i,j) pixels.[0,i,j] &lt;- lazy point.Value.R pixels.[1,i,j] &lt;- lazy point.Value.G pixels.[2,i,j] &lt;- lazy point.Value.B pixels </code></pre> <p><code>GetPixel</code> will be called at most once for every pixel, and then reused for the other components.</p> <p>Another way of approaching this problem would be to do a bulk-load of the entire image. This will be a lot quicker than calling <code>GetPixel</code> over and over again.</p> <pre><code>open System.Drawing open System.Drawing.Imaging let pixels (image:Bitmap) = let Width = image.Width let Height = image.Height let rect = new Rectangle(0,0,Width,Height) // Lock the image for access let data = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat) // Copy the data let ptr = data.Scan0 let stride = data.Stride let bytes = stride * data.Height let values : byte[] = Array.zeroCreate bytes System.Runtime.InteropServices.Marshal.Copy(ptr,values,0,bytes) // Unlock the image image.UnlockBits(data) let pixelSize = 4 // &lt;-- calculate this from the PixelFormat // Create and return a 3D-array with the copied data Array3D.init 3 Width Height (fun i x y -&gt; values.[stride * y + x * pixelSize + i]) </code></pre> <p>(adopted from the C# sample on <a href="http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx" rel="nofollow noreferrer" title="Bitmap.LockBits Method (Rectangle, ImageLockMode, PixelFormat)"><code>Bitmap.LockBits</code></a>)</p>
 

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