Masks an image and returns a two color bitmap. : Color « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » 2D Graphics » Color 




Masks an image and returns a two color bitmap.
  

using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Collections.Generic;

namespace Sqt.VisualStudio.Util
{
  /// <summary>
  /// Helpers for images and bitmap handling.
  /// </summary>
  internal static class ImageHelper
  {
    /// <summary>
    /// Loads an embedded resource image.
    /// </summary>
    /// <param name="resourceAssembly">The resource assembly usually the assembly of the code that calls this method.</param>
    /// <param name="resourcePath">The resource path.</param>
    /// <returns></returns>
    internal static Image LoadEmbeddedImageResourceAssembly resourceAssembly, string resourcePath )
    {
      if String.IsNullOrEmptyresourcePath ) ) 
        throw new ArgumentNullException"resourcePath" );

      Stream stream = resourceAssembly.GetManifestResourceStreamresourcePath );
      if stream == null ) {
        throw new ArgumentException"Can't find the resource " + resourcePath, "resourcePath" );
      }
      Image bitmap = Image.FromStreamstream );
      return bitmap;
    }


    /// <summary>
    /// Masks an image and returns a two color bitmap.
    /// </summary>
    /// <param name="image">The image to convert to a maskColor.</param>
    /// <param name="maskColor">The color used for masking.</param>
    /// <param name="unmaskColor">
    /// The color used when an image pixel has not a masking color.
    /// If the value is <see cref="P:Color.Empty"/> the original color
    /// will be used.
    /// </param>
    /// <param name="colorsToMask">The colors to replace with the maskColor color.</param>
    /// <returns></returns>
    internal static Image GetMaskImage image, Color maskColor, Color unmaskColor, params Color[] colorsToMask )
    {
      if image == null )
        throw new ArgumentNullException"image" );

      if colorsToMask == null )
        throw new ArgumentNullException"colorsToMask" );

      List<Color> colorsToMaskList = new List<Color>colorsToMask );
      bool isUnmaskDefined = unmaskColor != Color.Empty;
      Bitmap bitmap = new Bitmapimage );
      EqualColorPredicate equalColorPredicate = new EqualColorPredicate();

      for int y = 0; y < bitmap.Height; y++ ) {
        for int x = 0; x < bitmap.Width; x++ ) {
          Color pixel = bitmap.GetPixelx, y );
          equalColorPredicate.Pixel = pixel;

          if colorsToMaskList.ExistsequalColorPredicate.Matches ) )
            bitmap.SetPixelx, y, maskColor );
          else if isUnmaskDefined )
            bitmap.SetPixelx, y, unmaskColor );
        }
      }

      return bitmap;
    }

    /// <summary>
    /// Helper predicate to check color equality.
    /// </summary>
    /// <remarks>
    /// Its not possibile to use <see cref="M:Color.Equals"/>
    /// because the methode returns <c>false</c> if one of the colors
    /// compared is a named color and the other not, even if their
    /// RGB and alpha values are the same.
    /// </remarks>
    private class EqualColorPredicate
    {
      private Color pixel;

      public EqualColorPredicate()
      {
      }

      public Color Pixel
      {
        get return this.pixel; }
        set this.pixel = value; }
      }

      public bool MatchesColor color )
      {
        return color.R == Pixel.R && color.G == Pixel.G 
            && color.B == Pixel.B && color.A == Pixel.A;
      }
    }

  }
}

   
    
  














Related examples in the same category
1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Color to Hue,Lightness,Saturation value
27.Get Color From String
28.Convert String value To Color
29.Color to String
30.Convert Hex To Color
31.Create Color From HLS value
32.Parse Color with Color.FromArgb
33.Create Color Object from RGB value
34.Get Rainbow Colors
35.Convert RGB to HSL
36.HSL to RGB conversion.
37.Hsv To Rgb
38.Rgb Linear Interpolate
39.Get Luminance
40.To Grey scale
41.Heat Map background colour calculations
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.