CSharp examples for System.Drawing:Image Effect
Bitmap dilation
/*//from w ww . jav a2s . com Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ using System.Drawing.Imaging; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Drawing; using System; public class Main{ /// <summary> /// Does dilation /// </summary> /// <param name="Image">Image to manipulate</param> /// <param name="Size">Size of the aperture</param> public static Bitmap Dilate(Bitmap Image, int Size) { System.Drawing.Bitmap TempBitmap = Image; System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height); System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap); NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel); NewGraphics.Dispose(); Random TempRandom = new Random(); int ApetureMin = -(Size / 2); int ApetureMax = (Size / 2); for (int x = 0; x < NewBitmap.Width; ++x) { for (int y = 0; y < NewBitmap.Height; ++y) { int RValue = 0; int GValue = 0; int BValue = 0; for (int x2 = ApetureMin; x2 < ApetureMax; ++x2) { int TempX = x + x2; if (TempX >= 0 && TempX < NewBitmap.Width) { for (int y2 = ApetureMin; y2 < ApetureMax; ++y2) { int TempY = y + y2; if (TempY >= 0 && TempY < NewBitmap.Height) { Color TempColor = TempBitmap.GetPixel(TempX, TempY); if (TempColor.R > RValue) RValue = TempColor.R; if (TempColor.G > GValue) GValue = TempColor.G; if (TempColor.B > BValue) BValue = TempColor.B; } } } } Color TempPixel = Color.FromArgb(RValue, GValue, BValue); NewBitmap.SetPixel(x, y, TempPixel); } } return NewBitmap; } /// <summary> /// Does dilation /// </summary> /// <param name="FileName">Image to manipulate</param> /// <param name="Size">Size of the aperture</param> public static Bitmap Dilate(string FileName, int Size) { if (!IsGraphic(FileName)) return new Bitmap(1, 1); System.Drawing.Image TempImage = System.Drawing.Image.FromFile(FileName); System.Drawing.Bitmap TempBitmap = new System.Drawing.Bitmap(TempImage, TempImage.Width, TempImage.Height); System.Drawing.Bitmap ReturnBitmap = Image.Dilate(TempBitmap, Size); TempBitmap.Dispose(); TempImage.Dispose(); return ReturnBitmap; } /// <summary> /// Does dilation /// </summary> /// <param name="FileName">Image to manipulate</param> /// <param name="NewFileName">Location to save the image to</param> /// <param name="Size">Size of the aperture</param> public static void Dilate(string FileName, string NewFileName, int Size) { if (!IsGraphic(FileName)) return; ImageFormat FormatUsing = GetFormat(NewFileName); System.Drawing.Bitmap NewBitmap = Image.Dilate(FileName, Size); NewBitmap.Save(NewFileName, FormatUsing); NewBitmap.Dispose(); } }