什么是有效的方法来判断一个位图完全是黑色的? [英] What's an efficient way to tell if a bitmap is entirely black?

查看:333
本文介绍了什么是有效的方法来判断一个位图完全是黑色的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有确认的Image对象引用了一个全黑图像的超高效的方式,因此该位图中的每个像素都是ARGB(255,0,0,0)。

I'm wondering if there's a super-efficient way of confirming that an Image object references an entirely black image, so every pixel within the bitmap is ARGB(255, 0, 0, 0).

你会怎么推荐?大多数这些位图将是1024×6000像素(虽然它不是安全的假设,他们将永远是大小)。

What would you recommend? Most of these bitmaps will be 1024 x 6000 pixels (although it's not safe to assume they'll always be that size).

我需要这个,因为我们遇到与PrintWindow API的问题。我们发现,近20%的时间,在图像的至少一些部分将是一个黑色的方形(后续捕获将成功)。我的想法来解决,这是调用PrintWindow或WM_PRINT每个子窗口,然后一片窗的整体形象重新走到一起。如果我能找到的检测PrintWindow返回的黑色图像的特定的子窗口的有效途径,那么我可以很快再次捕捉调用PrintWindow。它很烂,但是PrintWindow是拍摄上的所有窗口工作的窗口(我想,反正)的唯一方法,并且支持拍摄被隐藏和/或关闭屏幕窗口。

I need this because we're having problems with the PrintWindow API. We find that nearly 20% of the time, at least some part of the image will be a black square (a subsequent capture will succeed). My idea to work around this was to call PrintWindow or WM_PRINT with each child window, then piece the whole image of the window back together. If I can find an efficient way of detecting that PrintWindow returned a black image for a particular child window, then I can quickly call PrintWindow again on that capture. It sucks, but PrintWindow is the only method of capturing a window that works on all windows (that I want, anyway) and supports capturing windows that are hidden and/or off-screen.

当PrintWindow失败,它不设置错误code或返回任何表明它失败了。当它有这个黑色方块的问题,它总是返回黑人整个窗口或子窗口。因此,通过单独捕获每一个子窗口,我可以肯定,我的每一个捕获的将工作过,只要它至少包含一个非黑色像素。

When PrintWindow fails, it doesn't set an error code or return anything that indicates it failed. When it has this black square problem, it's always an entire window or child window that returns black. So by capturing each child window separately, I can be sure that each of my captures will have worked, providing it contains at least one non-black pixel.

PrintWindow是Vista和上述,显然,但在这种情况下,我们限于服务器2003

PrintWindow is better in Vista and above, apparently, but in this case we're limited to Server 2003.

推荐答案

我建议你锁定在使用System.Drawing.Bitmap类型的LockBits方法内存中的位图。此方法返回的BitmapData类型,从中可以得到一个指向锁定的内存区域。然后通过存储器迭代,搜索非零字节(真,通过扫描所述的Int32甚至Int64值,根据所使用的平台上更快)。
code将是这样的:

I'd recommend you to lock the bitmap in the memory using the LockBits method of the System.Drawing.Bitmap type. This method returns the BitmapData type, from which you can receive a pointer to the locked memory region. Then iterate through the memory, searching for the non-zero bytes (really, faster by scanning for the Int32 or even Int64 values, depending on the platform you use). Code will look like this:

// Lock the bitmap's bits.  
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData =bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
int bytes  = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, bytes);

// Scanning for non-zero bytes
bool allBlack = true;
for (int index = 0; index < rgbValues.Length; index++)
    if (rgbValues[index] != 0) 
    {
       allBlack = false;
       break;
    }
// Unlock the bits.
bmp.UnlockBits(bmpData);

考虑使用不安全code和直接内存访问(使用指针),以提高性能。

Consider using the unsafe code and direct memory access (using pointers) to improve performance.

这篇关于什么是有效的方法来判断一个位图完全是黑色的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆