本文介绍了确定是否Alpha通道被用在图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我带来的图像到我的计划,我希望确定:

As I'm bringing in images into my program, I want to determine if:

  1. 在他们有一个alpha通道
  2. 如果是alpha通道时

1 是使用非常简单 Image.IsAlphaPixelFormat 。对于 2 但是,不是通过每一个像素循环等,有没有简单的方法可以让我确定像素的至少一个具有设置为比其他一些值,用来Alpha通道(即 255 )?所有我需要的背面是一个布尔值,然后我会做出决定是否要进行保存,以32位或24位。

#1 is simple enough with using Image.IsAlphaPixelFormat. For #2 though, other than looping through every single pixel, is there a simple way I can determine if at least one of the pixels has an alpha channel that is used (i.e. set to some other value than 255)? All I need back is a boolean and then I'll make determination as to whether to save it out to 32-bit or 24-bit.

更新:我发现的应该提供给我,我要找的 - 不幸的是,它并没有在所有的工作。例如,PNG格式有至少有阿尔法66(半透明)通道的像素格式继续报告(用法: IF((IMG .Flags和放大器; ImageFlags.HasTranslucent)== 4)...; )。我已经在所有类型的图像进行测试,包括:.BMP有alpha值> 0和< 255,它仍然报告。任何人都曾经使用过这一点,知道它甚至可以在GDI +?

UPDATE: I have discovered that ImageFlags.HasTranslucent should provide me with what I'm looking for - unfortunately, it doesn't work at all. For example, PNGs with pixel formats that have at least alpha channel of 66 (semi-transparent) continue to report False (Usage: if((img.Flags & ImageFlags.HasTranslucent) == 4) ...;). I've tested on all types of images, including .bmp that have an alpha value >0 and <255 and it still reports False. Anyone ever use this and know if it even works in GDI+?

推荐答案

您不必通过每一个像素循环(当然你可能会,但要看在图像上)。设置遍历所有的像素,而只是打破的循环,当你找到一个α值大于255,请使用以下伪code等:

You don't have to loop through every pixel (well you might, but it depends on the image). Set up to loop over all the pixels, but just break out of the loop when you find an alpha value other than 255 use the following pseudo code:

bool hasAlpha = false;
foreach (var pixel in image)
{
    hasAlpha = pixel.Alpha != 255;
    if (hasAlpha)
    {
        break;
    }
}

你只需要检查所有的像素没有任何Alpha图像。对于那些确实有阿尔法,这将打破很快图像。

You'll only have to check all the pixels for images that don't have any alpha. For images that do have alpha this will break out quite quickly.

这篇关于确定是否Alpha通道被用在图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:13
查看更多