//暗角 public static BitmapImage Vignetting(BitmapImage bitmapImage) { Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); int width = bitmap.Width; int height = bitmap.Height; double cx = width / 2; double cy = height / 2; double maxdist = cx * cx + cy * cy; double currdist = 0, factor; Color pixel; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { currdist = (i - cx) * (i - cx) + (j - cy) * (j - cy); factor = currdist / maxdist; pixel = bitmap.GetPixel(i, j); int red = (int)(pixel.R * (1 - factor)); int green = (int)(pixel.G * (1 - factor)); int blue = (int)(pixel.B * (1 - factor)); bitmap.SetPixel(i, j, Color.FromArgb(red, green, blue)); } } return(HelpClass.Bitmap2BitmapImage(bitmap)); }
//扩散 public static BitmapImage Biffusion(BitmapImage bitmapImae) { Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImae); int red, green, blue; int flag = 0; Color pixel; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { Random random = new Random(); int RankKey = random.Next(-5, 5); if (i + RankKey >= bitmap.Width || j + RankKey >= bitmap.Height || i + RankKey < 0 || j + RankKey < 0) { flag = 1; continue; } pixel = bitmap.GetPixel(i + RankKey, j + RankKey); red = (int)(pixel.R); green = (int)(pixel.G); blue = (int)(pixel.B); bitmap.SetPixel(i, j, Color.FromArgb(red, green, blue)); } } return(HelpClass.Bitmap2BitmapImage(bitmap)); }
private void HistTool_Click(object sender, RoutedEventArgs e) { BitmapImage bitmapImage = targetImage.Source as BitmapImage; Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); MenuItem mi = e.Source as MenuItem; string commond = mi.Header.ToString(); if (commond == "灰度直方图") { Hist_Form hist_Form = new Hist_Form(bitmap); hist_Form.Show(); } }
//去色 //gray = 0.3 * R + 0.59 * G + 0.11 * B public static BitmapImage Decolorize(BitmapImage bitmapImage) { Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); int gray; Color pixel; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { pixel = bitmap.GetPixel(i, j); gray = (int)(0.3 * pixel.R + 0.59 * pixel.G + 0.11 * pixel.B); bitmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray)); } } return(HelpClass.Bitmap2BitmapImage(bitmap)); }
//浮雕 public static BitmapImage Relief(BitmapImage bitmapImage) { Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); int red, green, blue; Color pixel; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { pixel = bitmap.GetPixel(i, j); red = (int)(255 - pixel.R); green = (int)(255 - pixel.G); blue = (int)(255 - pixel.B); bitmap.SetPixel(i, j, Color.FromArgb(red, green, blue)); } } return(HelpClass.Bitmap2BitmapImage(bitmap)); }
//马赛克 public static BitmapImage Mosaic(BitmapImage bitmapImage) { Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); int RIDIO = 50; for (int h = 0; h < bitmap.Height; h += RIDIO) { for (int w = 0; w < bitmap.Width; w += RIDIO) { int avgRed = 0, avgGreen = 0, avgBlue = 0; int count = 0; for (int x = w; (x < w + RIDIO && x < bitmap.Width); x++) { for (int y = h; (y < h + RIDIO && y < bitmap.Height); y++) { Color pixel = bitmap.GetPixel(x, y); avgRed += pixel.R; avgGreen += pixel.G; avgBlue += pixel.B; count++; } } avgRed = avgRed / count; avgGreen = avgGreen / count; avgBlue = avgBlue / count; for (int x = w; (x < w + RIDIO && x < bitmap.Width); x++) { for (int y = h; (y < h + RIDIO && y < bitmap.Height); y++) { bitmap.SetPixel(x, y, Color.FromArgb(avgRed, avgGreen, avgBlue)); } } } } return(HelpClass.Bitmap2BitmapImage(bitmap)); }
//文件菜单 private void FileMenuItem_Click(object sender, RoutedEventArgs e) { MenuItem mi = e.Source as MenuItem; string commond = mi.Header.ToString(); if (commond == "打开图片") { OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif"; //openFileDialog.Title = "打开图像文件"; openFileDialog.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" + "*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" + "位图(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" + "矢量图(*.wmf;*.eps;*.emf;...)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf"; //设置对话框标题 openFileDialog.Title = "打开图像文件"; if ((bool)openFileDialog.ShowDialog()) { originImage.Source = new BitmapImage(new Uri(openFileDialog.FileName)); } } else if (commond == "另存") { BitmapImage bitmapImage = targetImage.Source as BitmapImage; Bitmap bitmap = HelpClass.BitmapImage2Bitmap(bitmapImage); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "保存为"; saveFileDialog.OverwritePrompt = true; saveFileDialog.Filter = "BMP文件(*.bmp)|*.bmp|" + "Gif文件(*.gif)|*.gif|" + "JPEG文件(*.jpg)|*.jpg|" + "PNG文件(*.png)|*.png"; if ((bool)saveFileDialog.ShowDialog()) { string fileName = saveFileDialog.FileName; string strFileExtn = fileName.Remove(0, fileName.Length - 3); switch (strFileExtn) { case "bmp": bitmap.Save(fileName, ImageFormat.Bmp); break; case "jpg": bitmap.Save(fileName, ImageFormat.Jpeg); break; case "gif": bitmap.Save(fileName, ImageFormat.Gif); break; case "tif": bitmap.Save(fileName, ImageFormat.Tiff); break; case "png": bitmap.Save(fileName, ImageFormat.Png); break; default: break; } } } else { this.Close(); } }