【c#】编写代码给一个黑底灰色照片人脸部分添加滤镜

编写代码给一个黑底灰色照片人脸部分添加滤镜

结果展示:
通过将原图的人脸部分提取出来,对其进行红绿蓝分量的增加,达到添加滤镜的效果。
在这里插入图片描述
代码(c#):
test.cs 文件

using System;
using System.Drawing;
using System.Drawing.Imaging;
using tools;

namespace 添加RGB滤镜 // 文件名
{
    internal class test  //类名
    {
        static void Main(string[] args) 
        {
            string inputImagePath = @"D:\Visio Studio2022\添加RGB滤镜\0.jpg"; // 输入图片路径
            string outputImagePath = @"D:\Visio Studio2022\添加RGB滤镜\0.Jpeg"; // 输出图片路径
            //打开图像文件
            using (Bitmap Imageface = new Bitmap(inputImagePath))
            {
                Tools.showImage(Imageface);
                Color color = Color.Yellow;
                String s = null;
                Console.WriteLine("请输入添加滤镜的颜色(R/G/B):");
                s = Console.ReadLine(); //键盘输入
                switch (s)
                {
                    case "R":
                        color = Color.Red;break;
                    case "G":
                        color = Color.Green; break;
                    case "B":
                        color = Color.Blue; break;
                    default:  break;
                }
                //调用人像处理函数
                Bitmap imageout = Tools.ImageFace(Imageface,color);
                //保存修改后图片
                imageout.Save(outputImagePath, ImageFormat.Jpeg);
                //展示结果照片
                Tools.showImage(imageout);
            }
            //Console.ReadLine(); //键盘输入
        }
    }
}

Tools.cs 文件

using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace tools
{
    public class Tools
    {
        /// <summary>
        /// 给图片添加滤镜
        /// </summary>
        /// <param name="image"></param>
        /// <param name="rGb"></param>
        /// <returns>输出添加过滤镜的图片</returns>
        public static Bitmap ImageFace(Bitmap image, Color rGb)
        {
            try
            {
                Color targetColor = Color.Black; // 要替换的颜色
                //遍历每一个像素
                for (int y = 0; y < image.Height; y++){
                    for (int x = 0; x < image.Width; x++){
                        Color pixelColor = image.GetPixel(x, y);
                        if (pixelColor.ToArgb() > targetColor.ToArgb() + 500000)
                        //if (Math.Abs(pixelColor) != Math.Abs(targetColor))
                        //if (!AreColorsEqual(pixelColor, targetColor)) // 不等于黑色
                        {
                            if (rGb == Color.Green) {
                                int newGreen = Math.Min(pixelColor.G + 50, 255); // 增强绿色分量
                                Color newColor = Color.FromArgb(pixelColor.A, pixelColor.R, newGreen, pixelColor.B);
                                image.SetPixel(x, y, newColor);
                            }
                            else if (rGb == Color.Red){
                                int newred = Math.Min(pixelColor.G + 150, 255); // 增强红色分量
                                Color newColor = Color.FromArgb(pixelColor.A, newred, pixelColor.G, pixelColor.B);
                                image.SetPixel(x, y, newColor);
                            }
                            else if (rGb == Color.Blue){
                                int newblue = Math.Min(pixelColor.G + 50, 255); // 增强绿色分量
                                Color newColor = Color.FromArgb(pixelColor.A, pixelColor.R, pixelColor.G, newblue);
                                image.SetPixel(x, y, newColor);
                            }
                        }
                    }
                }
                Console.WriteLine("滤镜添加完成!");
            }
            catch (Exception ex) {
                Debug.WriteLine(ex.Message);
            }
            return image;
        }

        /// <summary>
        /// 用窗体输出图片
        /// </summary>
        /// <param name="imageout"></param>
        public static void showImage(System.Drawing.Image imageout)
        {
            try{
                // 创建一个窗体来显示图像
                using (Form displayForm = new Form()){
                    displayForm.Text = "Filtered Image";
                    displayForm.Size = new Size(imageout.Width, imageout.Height);
                    // 创建一个PictureBox控件用于显示图像
                    PictureBox pictureBox = new PictureBox();
                    pictureBox.Dock = DockStyle.Fill;
                    pictureBox.Image = imageout;
                    // 将PictureBox添加到窗体中
                    displayForm.Controls.Add(pictureBox);
                    // 显示窗体
                    Application.Run(displayForm);
                }
            }
            catch (Exception ex){
                Debug.WriteLine(ex.Message);
            }
        }
    }
}

学习总结:
在主函数中尽量不要写方法,将方法写在一个工具文件夹,使用时直接进行调用。有利于代码维护与功能的拓展。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值