troubleshooting Question

Foreach, Or Not Foreach.

Avatar of derekthornton
derekthornton asked on
C#
32 Comments1 Solution473 ViewsLast Modified:
I've got this method ..or rather series of methods that cycles through every pixel in an image using unmanaged code. One of which is displayed below ( this particular one, in conjunction with some other methods, checks a page to see if it is blank or has anything on it ).
I'd like to come up with a way to just have a template-like method stored in a .dll file, and then cycle through all the pixels using a Foreach loop on an image. For instance..

foreach(UnmanagedPixel p in iImage)
{
 ...
}

...Is this even possible? I've got no idea where to even begin to attempt something like this. Here's the sample method.

            public static Boolean ValidatePage(Image imgImage)
            {
                  // Store our blank pixel count
                  double dblBlankPixels = 0.0;
                  // Store our pixel count
                  double dblTotalPixels = 0.0;
                  // Work with a Bitmap copy
                  Bitmap bmImage = (Bitmap)imgImage.Clone();

                  // Lock the bits
                  BitmapData bmData = bmImage.LockBits
                        (new Rectangle(Point.Empty,bmImage.Size),
                        ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);

                  // Retrieve the Bitmap Stride Width
                  Int32 Stride = bmData.Stride;
                  // Create Unmanaged memory for this code
                  IntPtr Scan0 = bmData.Scan0;

                  unsafe
                  {
                        byte * p = (byte *)(void *)Scan0;

                        Int32 nOffset = Stride  - bmImage.Width * 3;
                        Int32 nWidth  = bmImage.Width * 3;

                        for(int y = 0; y < bmImage.Height; ++y)
                        {
                              for(int x=0; x < nWidth; ++x )
                              {
                                    if(p[0] >= (byte)(225))
                                    {
                                          dblBlankPixels++;
                                    }
                                    dblTotalPixels++;
                                    ++p;
                              }
                              p += nOffset;
                        }
                  }
                  // Unlock the bitmap
                  bmImage.UnlockBits(bmData);
                  // Destroy it
                  bmImage.Dispose();

                  return ComparePixelData(dblBlankPixels,dblTotalPixels);
            }
ASKER CERTIFIED SOLUTION
AlexFM

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 32 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 32 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros