C# 利用AForge.NET开源类库实现 图像素描效果

2021-05-16 20:30

阅读:604

标签:thread   ret   ppa   idt   action   cti   temp   main   control   

引入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
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    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开源类库实现 图像素描效果

标签:thread   ret   ppa   idt   action   cti   temp   main   control   

原文地址:https://www.cnblogs.com/wgscd/p/11792100.html


评论


亲,登录后才可以留言!