桌面应用程序——图片特效

毕业工作5年后,你决定利用所掌握的技术,自主创业。你决定开发一款图片特效处理的软件,该软件完成如下功能:

  1. 该软件包含一个主窗体,该主窗体的标题为“图片特效”。

  2. 该窗体包括4个PictureBox,4个按钮:4个按钮分别完成利用OpenfileDialog载入一张原始图片,并实现底片效果,灰度效果,以及模糊效果。对应效果如下图所示:

  3. 底片效果公式:255 - R, 255 – G, 255-B;

  4. 灰度效果公式:R * 0.299 + G * 0.587 + B * 0.114;

  5. 模糊效果原理: 求取原图像上x,y相邻的9个像素的颜色平均值,当作新的x,y像素的值。—均值滤波

g = (f(x-1,y-1) + f(x,y-1)+ f(x+1,y-1) + f(x-1,y) + f(x,y) + f(x+1,y) + f(x-1,y+1) + f(x,y+1) + f(x+1,y+1))/9
提示:利用Bitmap类的GetPixel,SetPixel方法对图片的像素进行读取或写入。

代码部分:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace practice17
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        byte[] picturebytes;
        Image img;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file1 = new OpenFileDialog();
            file1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            file1.Filter = "图片|*.jpg";
            file1.Title = "请选择图片";
            if (file1.ShowDialog() == DialogResult.OK)
            {
                string path = file1.FileName;
                FileStream fs = new FileStream(path,FileMode.Open);
                picturebytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                picturebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
                MemoryStream ms = new MemoryStream(picturebytes);
                Bitmap btmp = new Bitmap(fs);
                img = btmp;
                pictureBox1.Image = img;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(img);
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    Color RGB = bmp.GetPixel(i, j);
                    int r = 255 - RGB.R;
                    int g = 255 - RGB.G;
                    int b = 255 - RGB.B;
                    bmp.SetPixel(i, j, Color.FromArgb(r,g,b));
                }
            }
            pictureBox2.Image = bmp;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(img);
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    Color RGB = bmp.GetPixel(i, j);
                    int r = (RGB.R + RGB.G + RGB.B) / 3;
                    bmp.SetPixel(i, j, Color.FromArgb(r,r,r));
                }
            }
            pictureBox3.Image = bmp;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(img);
            int[,] s1 = new int[bmp.Width, bmp.Height];
            for (int i = 0; i < bmp.Width-2; i++)
            {
                for (int j = 0; j < bmp.Height-2; j++)
                {
                    int RGB1 = bmp.GetPixel(i, j + 1).ToArgb();
                    int RGB2 = bmp.GetPixel(i, j + 2).ToArgb();
                    int RGB3 = bmp.GetPixel(i + 1, j).ToArgb();
                    int RGB4 = bmp.GetPixel(i + 1, j + 1).ToArgb();
                    int RGB5 = bmp.GetPixel(i + 1, j + 2).ToArgb();
                    int RGB6 = bmp.GetPixel(i + 2, j).ToArgb();
                    int RGB7 = bmp.GetPixel(i + 2, j + 1).ToArgb();
                    int RGB8 = bmp.GetPixel(i + 2, j + 2).ToArgb();
                    int RGB9 = bmp.GetPixel(i, j).ToArgb();
                    //s1[i, j] = RGB9;
                    s1[i + 1, j + 1] = (RGB1 + RGB2 + RGB3 + RGB4 + RGB5 + RGB6 + RGB7 + RGB8 + RGB9) / 9;
                    bmp.SetPixel(i+1, j+1, Color.FromArgb(s1[i+1,j+1]));
                }
            }
            pictureBox4.Image = bmp;
        }
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值