Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7.  
  8. namespace WindowsFormsApp5
  9. {
  10.     public static class ImageFilter
  11.     {
  12.         public static Image FilterImage(this Image inputImage, FilterStyle style)
  13.         {
  14.             Bitmap outputImage = new Bitmap(inputImage.Width, inputImage.Height);
  15.             Graphics G = Graphics.FromImage(outputImage);
  16.             G.DrawImage(inputImage, 0, 0);
  17.  
  18.             if (style == FilterStyle.SemiTransparentColor) { G.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Red)), 0, 0, outputImage.Width, outputImage.Height); }
  19.             if (style == FilterStyle.Vignette)
  20.             {
  21.                 Brush b;
  22.                 b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width / 2, outputImage.Height), Color.Black, Color.Transparent, 360);
  23.                 G.FillRectangle(b, 0, 0, outputImage.Width / 2, outputImage.Height);
  24.                 b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width / 2, outputImage.Height), Color.Transparent, Color.Black, 360);
  25.                 G.FillRectangle(b, outputImage.Width - outputImage.Width / 2, 0, outputImage.Width / 2, outputImage.Height);
  26.                 b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width, outputImage.Height / 2), Color.Black, Color.Transparent, 90);
  27.                 G.FillRectangle(b, 0, 0, outputImage.Width, outputImage.Height / 2);
  28.                 b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, outputImage.Width, outputImage.Height / 2), Color.Black, Color.Transparent, 270);
  29.                 G.FillRectangle(b, 0, outputImage.Height - outputImage.Height / 2 + 1, outputImage.Width, outputImage.Height / 2);
  30.             }
  31.             if (style == FilterStyle.GrayScale)
  32.             {
  33.                 for (Int32 y = 0; y < outputImage.Height; y++)
  34.                     for (Int32 x = 0; x < outputImage.Width; x++)
  35.                     {
  36.                         Color c = outputImage.GetPixel(x, y);
  37.  
  38.                         Int32 gs = (Int32)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
  39.  
  40.                         outputImage.SetPixel(x, y, Color.FromArgb(gs, gs, gs));
  41.                     }
  42.             }
  43.             if (style == FilterStyle.Invert)
  44.             {
  45.                 for (int y = 0; (y <= (outputImage.Height - 1)); y++)
  46.                 {
  47.                     for (int x = 0; (x <= (outputImage.Width - 1)); x++)
  48.                     {
  49.                         Color inv = outputImage.GetPixel(x, y);
  50.                         inv = Color.FromArgb(255, (255 - inv.R), (255 - inv.G), (255 - inv.B));
  51.                         outputImage.SetPixel(x, y, inv);
  52.                     }
  53.                 }
  54.             }
  55.             return outputImage;
  56.         }
  57.  
  58.         public enum FilterStyle
  59.         {
  60.             SemiTransparentColor, Vignette, GrayScale, Invert
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement