Image Filter art-detail-preserving polishing C # Program Implementation
Here, we also use the ZPhotoEngine library for implementation. After all, the effect in this library is almost the same as that in PS. For more information, I will give it at the end of the article, the steps for skin polishing are as follows:
1. blur the surface of copy a of the source image with a radius of 15;
2. Retain the source image in high contrast with a radius of 1.0;
3. Perform linear optical layer processing on the high contrast result and the source image, with 50% transparency;
Based on the above three steps, the main code of my skin polishing class is as follows:
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 =. lockBits (new Rectangle (0, 0,. width,. 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-. width * 4; int temp = 0; for (int j = 0; j <. height; j ++) {for (int I = 0; I <. width; I ++) {// Process image... // linear light layer mixing temp = zp. modeLinearLight (p [0], dstP [0]); // transparency 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;}. unlockBits (srcData); highPass. unlockBits (dstData); return highPass ;}}}
The main code of the interface is as follows:
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 variable declaration // image path private String curFileName = null; // The current image variable private Bitmap curBitmap = null ;/ /Original image variable private Bitmap srcBitmap = null; // ImageFilter imfilter = new ImageFilter (); # endregion # region image open storage module // open the image function public void OpenFile () {OpenFileDialog ofd = new OpenFileDialog (); ofd. filter = "all image files | *. bmp ;*. pcx ;*. png ;*. jpg ;*. gif; "+ "*. tif ;*. ico ;*. dxf ;*. cgm ;*. cdr ;*. wmf ;*. eps ;*. emf | "+" Bitmap (*. bmp ;*. jpg ;*. png ;...) | *. bmp ;*. pcx ;*. png ;*. jpg ;*. gif ;*. tif ;*. ico | "+" vector chart (*. Wmf ;*. eps ;*. emf ;...) | *. dxf ;*. cgm ;*. cdr ;*. wmf ;*. eps ;*. emf "; ofd. showHelp = true; ofd. title = "open image file"; 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) ;}}// Save the image function public void SaveFile () {SaveFileDialo G sfd = new SaveFileDialog (); sfd. filter = "PNG file (*. png) | *. png "; if (sfd. showDialog () = DialogResult. OK) {pictureBox1.Image. save (sfd. fileName, ImageFormat. png) ;}} // open the image private void openBtn_Click (object sender, EventArgs e) {OpenFile (); if (curBitmap! = Null) {pictureBox1.Image = (Image) curBitmap ;}// Save the Image private void saveBtn_Click (object sender, EventArgs e) {if (pictureBox1.Image! = Null) SaveFile () ;}# endregion // confirm 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 );}}}}}
The program interface is as follows:
Finally, put:
Source image C # Program
PS
You can compare the PS effect with that in this article. The difference is almost invisible to the naked eye.