Creates a new image, 2 pixels wider and taller than the original. Each pixel's alpha value is the largest alpha value among adjacent pixels on the original image. The color of each pixel will be the color given in the second argument. - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Creates a new image, 2 pixels wider and taller than the original. Each pixel's alpha value is the largest alpha value among adjacent pixels on the original image. The color of each pixel will be the color given in the second argument.

Demo Code


using System.Windows.Forms;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using System;//from w w  w . j  a  v a  2 s . c om

public class Main{
        /// <summary>
      /// Creates a new image, 2 pixels wider and taller than the original.
      /// Each pixel's alpha value is the largest alpha value among adjacent pixels on the original image.
      /// The color of each pixel will be the color given in the second argument.
      /// </summary>
      public static Bitmap Blur(Bitmap orig, Color blurColor) {
         Bitmap b = new Bitmap(orig.Width + 2, orig.Height + 2);
         for (int y = -1; y < orig.Height + 1; y++) {
            for (int x = -1; x < orig.Width + 1; x++) {
               byte[] vals = {TryGetAlpha(orig, x-1, y-1) ?? 0,
                           TryGetAlpha(orig, x-1, y) ?? 0,
                           TryGetAlpha(orig, x-1, y+1) ?? 0,
                           TryGetAlpha(orig, x, y-1) ?? 0,
                           TryGetAlpha(orig, x, y) ?? 0,
                           TryGetAlpha(orig, x, y+1) ?? 0,
                           TryGetAlpha(orig, x+1, y-1) ?? 0,
                           TryGetAlpha(orig, x+1, y) ?? 0,
                           TryGetAlpha(orig, x+1, y+1) ?? 0};
               byte alpha = vals.Max();
               b.SetPixel(x + 1, y + 1, Color.FromArgb(alpha, blurColor));
            }
         }
         return b;
      }
        /// <summary>
      /// Used for Blur.
      /// </summary>
      private static byte? TryGetAlpha(Bitmap b, int x, int y) {
         if (x < 0 || x >= b.Width || y < 0 || y >= b.Height) {
            return null;
         } else {
            return b.GetPixel(x, y).A;
         }
      }
}

Related Tutorials