C# 利用AForge.NET开源类库实现 图像素描效果
Posted wgscd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 利用AForge.NET开源类库实现 图像素描效果相关的知识,希望对你有一定的参考价值。
引入DLL:
using AForge.Imaging; using AForge.Imaging.Filters; //using AForge.Video.DirectShow;可以使用摄像头图像
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using AForge.Imaging; using AForge.Imaging.Filters; using AForge.Video.DirectShow; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Windows.Media; using System.Windows.Media.Imaging; namespace GrayImage { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { } private Bitmap image = null; private void Button_Click(object sender, RoutedEventArgs e) { GrayPhoto(20); } void GrayPhoto(int stepSize) { //素描效果制作 // image = (Bitmap)CurrentPhoto.SourceSource; image = GetBitmap(CurrentPhoto.Source as BitmapSource); if (image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) { Bitmap temp = AForge.Imaging.Image.Clone(image, System.Drawing.Imaging.PixelFormat.Format24bppRgb); image.Dispose(); image = temp; } //转换成灰度图像 Bitmap temBitmap = Grayscale.CommonAlgorithms.BT709.Apply(image); Bitmap temBitmap2 = (Bitmap)temBitmap.Clone(); Bitmap temBitmap3 = (Bitmap)temBitmap.Clone(); // create filter Invert filter = new Invert(); // apply the filter filter.ApplyInPlace(temBitmap2); // create filter 边缘提取 DifferenceEdgeDetector filterEdge = new DifferenceEdgeDetector(); // apply the filter filterEdge.ApplyInPlace(temBitmap); // create filter MoveTowards filterMove = new MoveTowards(temBitmap2, stepSize); // apply the filter Bitmap resultImage = filterMove.Apply(temBitmap); filter.ApplyInPlace(resultImage); BitmapSource temSource = CreateBitmapSourceFromBitmap(resultImage); CurrentPhoto.Source = temSource; } Bitmap GetBitmap(BitmapSource m) { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); System.Drawing.Imaging.BitmapData data = bmp.LockBits( new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data); return bmp; } BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap) { /*------------------------------------------------------------------------- //Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针, //返回一个托管的BitmapSource ---------------------------------------------------------------------------*/ IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象 BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); return bitmapSource; } } }
以上是关于C# 利用AForge.NET开源类库实现 图像素描效果的主要内容,如果未能解决你的问题,请参考以下文章