zoukankan      html  css  js  c++  java
  • 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;
    
    
            }
    
    
    
    
        }
    }
    

      

  • 相关阅读:
    加密算法
    oracle利用正则表达式判断字符串只包含数字
    LINUX下用数据泵导入导出(IMPDP、EXPDP)
    Oracle删除用户和表空间
    SQL脚本
    Oracle 查询库中所有表名、字段名、字段名说明,查询表的数据条数、表名、中文表名、
    Oracle修改表Table所属表空间及Clob、Blob字段的处理
    Linux下修改Oracle监听地址
    批量删除默认用户
    oracle创建新用户和用户表空间
  • 原文地址:https://www.cnblogs.com/wgscd/p/11792100.html
Copyright © 2011-2022 走看看