Checks if an image has varying RGB values among pixels that are partially or fully opaque. - CSharp System.Drawing

CSharp examples for System.Drawing:Image Operation

Description

Checks if an image has varying RGB values among pixels that are partially or fully opaque.

Demo Code


using System.Windows.Forms;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using System;/*ww  w .jav  a 2  s .  co m*/

public class Main{
        /// <summary>
        /// Checks if an image has varying RGB values among pixels that are partially or fully opaque.
        /// </summary>
        public static bool HasNonAlpha(Bitmap bmp) {
            int? found = null;
                  for (int y = 0; y < bmp.Height; y++) {
                     for (int x = 0; x < bmp.Width; x++) {
                Color color = bmp.GetPixel(x, y);
                if (color.A == 0) continue;
         
                int rgb = color.ToArgb() & 0xFFFFFF;
                        if (found == null) {
                     found = rgb;
                } else if (rgb != found) {
                     return true;
                }
                     }
                  }
                  return false;
      }
}

Related Tutorials