示例#1
0
        public static Bitmap AddShadow(Image sourceImage, float opacity, int size, float darkness, Color color, Point offset)
        {
            Image shadowImage = null;

            try
            {
                shadowImage = sourceImage.CreateEmptyBitmap(size * 2, size * 2);

                ColorMatrix maskMatrix = new ColorMatrix();
                maskMatrix.Matrix00 = 0;
                maskMatrix.Matrix11 = 0;
                maskMatrix.Matrix22 = 0;
                maskMatrix.Matrix33 = opacity;
                maskMatrix.Matrix40 = ((float)color.R).Remap(0, 255, 0, 1);
                maskMatrix.Matrix41 = ((float)color.G).Remap(0, 255, 0, 1);
                maskMatrix.Matrix42 = ((float)color.B).Remap(0, 255, 0, 1);

                Rectangle shadowRectangle = new Rectangle(size, size, sourceImage.Width, sourceImage.Height);
                maskMatrix.Apply(sourceImage, shadowImage, shadowRectangle);

                if (size > 0)
                {
                    Blur((Bitmap)shadowImage, size);
                }

                if (darkness > 1)
                {
                    ColorMatrix alphaMatrix = new ColorMatrix();
                    alphaMatrix.Matrix33 = darkness;

                    Image shadowImage2 = alphaMatrix.Apply(shadowImage);
                    shadowImage.Dispose();
                    shadowImage = shadowImage2;
                }

                Bitmap result = shadowImage.CreateEmptyBitmap(Math.Abs(offset.X), Math.Abs(offset.Y));

                using (Graphics g = Graphics.FromImage(result))
                {
                    g.SetHighQuality();
                    g.DrawImage(shadowImage, Math.Max(0, offset.X), Math.Max(0, offset.Y), shadowImage.Width, shadowImage.Height);
                    g.DrawImage(sourceImage, Math.Max(size, -offset.X + size), Math.Max(size, -offset.Y + size), sourceImage.Width, sourceImage.Height);
                }

                return result;
            }
            finally
            {
                if (sourceImage != null) sourceImage.Dispose();
                if (shadowImage != null) shadowImage.Dispose();
            }
        }
示例#2
0
        public override Image Apply(Image img)
        {
            ColorMatrix colorMatrix = new ColorMatrix(new[]
            {
                new float[] { Rr, Gr, Br, Ar, 0 },
                new float[] { Rg, Gg, Bg, Ag, 0 },
                new float[] { Rb, Gb, Bb, Ab, 0 },
                new float[] { Ra, Ga, Ba, Aa, 0 },
                new float[] { Ro, Go, Bo, Ao, 1 }
            });

            using (img)
            {
                return colorMatrix.Apply(img);
            }
        }
示例#3
0
        public static Image Outline(Image img, int borderSize, Color borderColor)
        {
            Bitmap result = img.CreateEmptyBitmap(borderSize * 2, borderSize * 2);

            ColorMatrix maskMatrix = new ColorMatrix();
            maskMatrix.Matrix00 = 0;
            maskMatrix.Matrix11 = 0;
            maskMatrix.Matrix22 = 0;
            maskMatrix.Matrix33 = 1;
            maskMatrix.Matrix40 = ((float)borderColor.R).Remap(0, 255, 0, 1);
            maskMatrix.Matrix41 = ((float)borderColor.G).Remap(0, 255, 0, 1);
            maskMatrix.Matrix42 = ((float)borderColor.B).Remap(0, 255, 0, 1);

            using (img)
            using (Image shadow = maskMatrix.Apply(img))
            using (Graphics g = Graphics.FromImage(result))
            {
                for (int i = 0; i <= borderSize * 2; i++)
                {
                    g.DrawImage(shadow, new Rectangle(i, 0, shadow.Width, shadow.Height));
                    g.DrawImage(shadow, new Rectangle(i, borderSize * 2, shadow.Width, shadow.Height));
                    g.DrawImage(shadow, new Rectangle(0, i, shadow.Width, shadow.Height));
                    g.DrawImage(shadow, new Rectangle(borderSize * 2, i, shadow.Width, shadow.Height));
                }

                g.DrawImage(img, new Rectangle(borderSize, borderSize, img.Width, img.Height));
            }

            return result;
        }
 /// <summary>
 /// Gets the Green filter for an image
 /// </summary>
 /// <param name="Image">Image to change</param>
 /// <param name="FileName">File to save to</param>
 /// <returns>A bitmap object</returns>
 public static Bitmap GreenFilter(this Bitmap Image, string FileName = "")
 {
     Image.ThrowIfNull("Image");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     ColorMatrix TempMatrix = new ColorMatrix();
     TempMatrix.Matrix = new float[][]{
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 1, 0, 0, 0},
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {0, 0, 0, 0, 1}
                 };
     Bitmap NewBitmap = TempMatrix.Apply(Image);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Converts an image to sepia tone
 /// </summary>
 /// <param name="Image">Image to change</param>
 /// <param name="FileName">File to save to</param>
 /// <returns>A bitmap object of the sepia tone image</returns>
 public static Bitmap SepiaTone(this Bitmap Image, string FileName = "")
 {
     Image.ThrowIfNull("Image");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     ColorMatrix TempMatrix = new ColorMatrix();
     TempMatrix.Matrix = new float[][]{
                     new float[] {.393f, .349f, .272f, 0, 0},
                     new float[] {.769f, .686f, .534f, 0, 0},
                     new float[] {.189f, .168f, .131f, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {0, 0, 0, 0, 1}
                 };
     Bitmap NewBitmap = TempMatrix.Apply(Image);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Adjusts the brightness
 /// </summary>
 /// <param name="Image">Image to change</param>
 /// <param name="FileName">File to save to</param>
 /// <param name="Value">-255 to 255</param>
 /// <returns>A bitmap object</returns>
 public static Bitmap AdjustBrightness(this Bitmap Image, int Value = 0, string FileName = "")
 {
     Image.ThrowIfNull("Image");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     float FinalValue = (float)Value / 255.0f;
     ColorMatrix TempMatrix = new ColorMatrix();
     TempMatrix.Matrix = new float[][]{
                     new float[] {1, 0, 0, 0, 0},
                     new float[] {0, 1, 0, 0, 0},
                     new float[] {0, 0, 1, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
                 };
     Bitmap NewBitmap = TempMatrix.Apply(Image);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Gets the Green filter for an image
 /// </summary>
 /// <param name="Image">Image to change</param>
 /// <param name="FileName">File to save to</param>
 /// <returns>A bitmap object</returns>
 public static Bitmap GreenFilter(this Bitmap Image, string FileName = "")
 {
     Contract.Requires<ArgumentNullException>(Image != null, "Image");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     ColorMatrix TempMatrix = new ColorMatrix();
     TempMatrix.Matrix = new float[][]{
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 1, 0, 0, 0},
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {0, 0, 0, 0, 1}
                 };
     Bitmap NewBitmap = TempMatrix.Apply(Image);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }
 /// <summary>
 /// Gets the blue filter for an image
 /// </summary>
 /// <param name="Image">Image to change</param>
 /// <param name="FileName">File to save to</param>
 /// <returns>A bitmap object</returns>
 public static Bitmap BlueFilter(this Bitmap Image, string FileName = "")
 {
     if (Image == null)
         throw new ArgumentNullException("Image");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     ColorMatrix TempMatrix = new ColorMatrix();
     TempMatrix.Matrix = new float[][]{
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 0, 0, 0, 0},
                     new float[] {0, 0, 1, 0, 0},
                     new float[] {0, 0, 0, 1, 0},
                     new float[] {0, 0, 0, 0, 1}
                 };
     Bitmap NewBitmap = TempMatrix.Apply(Image);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }