C# 读取图像->改色调色->输出

改色方案一:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        // Retrieve the image.【读取图像】
        Bitmap image1 = new Bitmap(@"输入图像文件", true); //"true" to use color correction for this Bitmap; otherwise, "false".
        pictureBox1.Image = image1;
        pictureBox1.Refresh();  //更新显示内容
        int x, y;

        // Loop through the images pixels to reset color.【改色、调色】
        for (x = 0; x < 200; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0); //纯红
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 200; x < 400; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(0, pixelColor.G, 0);  //纯绿
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 400; x < 600; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(0, 0, pixelColor.B);  //纯蓝
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 600; x < 800; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.B, 0, 0);  //蓝转红
                image1.SetPixel(x, y, newColor);
            }
        }

        Random rand = new Random();
        int test = rand.Next(0, 255);
        for (x = 800; x < image1.Width; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));  //随机色(noise)
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image. 【输出】
        pictureBox1.Image = image1;
        pictureBox1.Refresh();
        image1.Save(@"输出图像文件.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        // Display the pixel format in Label1.
        MessageBox.Show("Pixel format: " + image1.PixelFormat.ToString());
    }
    catch (ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}

改色方案二:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        // Retrieve the image.【读取图像】
        Bitmap image1 = new Bitmap(@"输入图像文件", true); //"true" to use color correction for this Bitmap; otherwise, "false".
        pictureBox1.Image = image1;
        pictureBox1.Refresh();  //更新显示内容
        int x, y;

        // Loop through the images pixels to reset color.【改色、调色】
        for (x = 0; x < 200; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, 0);  //红绿
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 200; x < 400; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, pixelColor.B);  //红蓝
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 400; x < 600; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(0, pixelColor.G, pixelColor.B);  //绿蓝
                image1.SetPixel(x, y, newColor);
            }
        }

        for (x = 600; x < 800; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);  //红绿蓝
                image1.SetPixel(x, y, newColor);
            }
        }

        Random rand = new Random();
        int test = rand.Next(0, 255);
        for (x = 800; x < image1.Width; x++)
        {
            for (y = 0; y < image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(0, pixelColor.B, 0);  //蓝转绿
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image. 【输出】
        pictureBox1.Image = image1;
        pictureBox1.Refresh();
        image1.Save(@"输出图像文件.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        // Display the pixel format in Label1.
        MessageBox.Show("Pixel format: " + image1.PixelFormat.ToString());
    }
    catch (ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}

示例

输入图片:

改色方案一:

改色方案二:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值