映像濾鏡藝術---保留細節的磨皮之C#程式實現

來源:互聯網
上載者:User

標籤:src   return   exception   lte   dialog   簡介   generic   cbitmap   send   

上一篇博文“保留細節的磨皮濾鏡之PS實現”一文中。我簡介了本人自己總結的一種非常easy的磨皮濾鏡,這個濾鏡在磨光皮膚的同一時候,會保留非常不錯的細節,今天,我將介紹使用C#程式實現這個磨皮的過程。

這裡,我們相同是使用ZPhotoEngine庫來實現,畢竟這個庫中實現的效果跟PS是差點兒一模一樣的。關於,文章最後會給出。廢話不多說了,磨皮過程例如以下:

一。對原圖的副本a運行表面模糊,半徑15;

二。對原圖運行高反差保留,半徑1.0。

三。對高反差結果與原圖做線性光圖層處理,50%透明度就可以;

依據以上三步,我的磨皮類主要代碼例如以下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Drawing.Imaging;namespace TestDemo{    unsafe class ImageFilter    {        ZPhotoEngineDll zp = new ZPhotoEngineDll();        public Bitmap SoftSkinFilter(Bitmap src, int blurRadius)        {            //表面模糊圖層            Bitmap a = zp.SurfaceBlur(src, 28, blurRadius);            //高反差圖層            Bitmap highPass = zp.HighPassProcess(src, 1.0f);            BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);            BitmapData dstData = highPass.LockBits(new Rectangle(0, 0, highPass.Width, highPass.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);            byte* p = (byte*)srcData.Scan0;            byte* dstP = (byte*)dstData.Scan0;            int offset = srcData.Stride - a.Width * 4;            int temp = 0;            for (int j = 0; j < a.Height; j++)            {                for (int i = 0; i < a.Width; i++)                {                    ////////////////Process image...                    //線性光圖層混合                    temp = zp.ModeLinearLight(p[0], dstP[0]);                    //透明度50%                    dstP[0] = (byte)((p[0] + temp) >> 1);                    temp = zp.ModeLinearLight(p[1], dstP[1]);                    dstP[1] = (byte)((p[1] + temp) >> 1);                    temp = zp.ModeLinearLight(p[2], dstP[2]);                    dstP[2] = (byte)((p[2] + temp) >> 1);                    dstP += 4;                    p += 4;                }                dstP += offset;                p += offset;            }            a.UnlockBits(srcData);            highPass.UnlockBits(dstData);            return highPass;        }            }}
介面部分主要代碼例如以下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Imaging;namespace TestDemo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        #region  變數聲明        //映像路徑        private String curFileName = null;        //當前映像變數        private Bitmap curBitmap = null;        //原始映像變數        private Bitmap srcBitmap = null;        //        ImageFilter imfilter = new ImageFilter();        #endregion        #region  映像開啟儲存模組        //開啟映像函數        public void OpenFile()        {            OpenFileDialog ofd = new OpenFileDialog();            ofd.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";            ofd.ShowHelp = true;            ofd.Title = "開啟影像檔";            if (ofd.ShowDialog() == DialogResult.OK)            {                curFileName = ofd.FileName;                try                {                    curBitmap = (Bitmap)System.Drawing.Image.FromFile(curFileName);                    srcBitmap = new Bitmap(curBitmap);                }                catch (Exception exp)                { MessageBox.Show(exp.Message); }            }        }        //儲存映像函數        public void SaveFile()        {            SaveFileDialog sfd = new SaveFileDialog();            sfd.Filter = "PNG檔案(*.png)|*.png";            if (sfd.ShowDialog() == DialogResult.OK)            {                pictureBox1.Image.Save(sfd.FileName, ImageFormat.Png);            }        }        //開啟映像        private void openBtn_Click(object sender, EventArgs e)        {            OpenFile();            if (curBitmap != null)            {                pictureBox1.Image = (Image)curBitmap;            }        }        //儲存映像        private void saveBtn_Click(object sender, EventArgs e)        {            if (pictureBox1.Image != null)                SaveFile();        }        #endregion        //確定        private void okBtn_Click(object sender, EventArgs e)        {            if (pictureBox1.Image != null)            {                int radius =  Convert.ToInt32(textBox1.Text.ToString());                if (radius >= 0 && radius <= 20)                {                    pictureBox1.Image = (Image)imfilter.SoftSkinFilter(curBitmap, radius);                }            }        }    }}
程式介面例如以下:


最後,放上:


原圖                                                                                  C#程式


PS

大家能夠對照一下,PS效果跟本文實現效果是一模一樣的,區別差點兒是肉眼看不到的呵呵。

最後,放上一些下載串連:

1,ZPhotoEngine庫下載串連:點擊開啟連結

2,磨皮代碼DEMO免費下載串連:點擊開啟連結


映像濾鏡藝術---保留細節的磨皮之C#程式實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.