Apply Mask on Bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Image Operation

Description

Apply Mask on Bitmap

Demo Code


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

public class Main{
    public static Bitmap ApplyMask(Bitmap source, Bitmap mask) {
         source = Resize(source, mask.Size);
         Bitmap ret = new Bitmap(source.Width, source.Height);
         for (int x = 0; x < ret.Width; x++) {
            for (int y = 0; y < ret.Height; y++) {
               Color c = source.GetPixel(x, y);
               ret.SetPixel(x, y, Color.FromArgb(
                  mask.GetPixel(x, y).R,
                  c.R, c.G, c.B));
            }
         }
         return ret;
      }
        /// <summary>
      /// Makes a scaled version of an image using BrawlLib's texture converter.
      /// </summary>
      public static Bitmap Resize(Bitmap orig, Size resizeTo) {
         return orig.Resize(resizeTo.Width, resizeTo.Height);
      }
}

Related Tutorials