Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- namespace WindowsFormsApp5
- {
- public static class ImageFilter
- {
- public static Image FilterImage(this Image inputImage, FilterStyle style)
- {
- Bitmap outputImage = new Bitmap(inputImage.Width, inputImage.Height);
- Graphics G = Graphics.FromImage(outputImage);
- G.DrawImage(inputImage, 0, 0);
- if (style == FilterStyle.SemiTransparentColor) { G.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Red)), 0, 0, outputImage.Width, outputImage.Height); }
- if (style == FilterStyle.Vignette)
- {
- Brush b;
- b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width / 2, outputImage.Height), Color.Black, Color.Transparent, 360);
- G.FillRectangle(b, 0, 0, outputImage.Width / 2, outputImage.Height);
- b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width / 2, outputImage.Height), Color.Transparent, Color.Black, 360);
- G.FillRectangle(b, outputImage.Width - outputImage.Width / 2, 0, outputImage.Width / 2, outputImage.Height);
- b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width, outputImage.Height / 2), Color.Black, Color.Transparent, 90);
- G.FillRectangle(b, 0, 0, outputImage.Width, outputImage.Height / 2);
- b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width, outputImage.Height / 2), Color.Black, Color.Transparent, 270);
- G.FillRectangle(b, 0, outputImage.Height - outputImage.Height / 2 + 1, outputImage.Width, outputImage.Height / 2);
- }
- if (style == FilterStyle.GrayScale)
- {
- for (Int32 y = 0; y < outputImage.Height; y++)
- for (Int32 x = 0; x < outputImage.Width; x++)
- {
- Color c = outputImage.GetPixel(x, y);
- Int32 gs = (Int32)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
- outputImage.SetPixel(x, y, Color.FromArgb(gs, gs, gs));
- }
- }
- if (style == FilterStyle.Invert)
- {
- for (int y = 0; (y <= (outputImage.Height - 1)); y++)
- {
- for (int x = 0; (x <= (outputImage.Width - 1)); x++)
- {
- Color inv = outputImage.GetPixel(x, y);
- inv = Color.FromArgb(255, (255 - inv.R), (255 - inv.G), (255 - inv.B));
- outputImage.SetPixel(x, y, inv);
- }
- }
- }
- return outputImage;
- }
- public enum FilterStyle
- {
- SemiTransparentColor, Vignette, GrayScale, Invert
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement