C#对图像重新着色

C#对图像重新着色


1、Bitmap

构造函数

属性

  • Height获取此 Image 的高度(以像素为单位)。 (继承自 Image
  • Size获取此图像的以像素为单位的宽度和高度。
  • Width 获取此 Image 的宽度(以像素为单位)。 (继承自 Image。)
  • PixelFormat:获取此 Image 的像素格式。 (继承自 Image。)

方法

  • Clone()创建此 Image 的一个精确副本。 (继承自 Image。)
  • GetPixel:获取此 Bitmap 中指定像素的颜色。
  • Save(String, ImageFormat):将此 Image 以指定格式保存到指定文件。 (继承自 Image。)
  • SetPixel:设置此 Bitmap 中指定像素的颜色。

2、Color 结构

表示一种 ARGB 颜色(alpha、红色、绿色、蓝色)。

属性

  • R获取此 Color 结构的红色分量值。
  • G获取此 Color 结构的绿色分量值。

方法

  • FromArgb(Int32)从一个 32 位 ARGB 值创建 Color 结构。
  • FromArgb(Int32, Int32, Int32, Int32)从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构。 尽管此方法允许为每个分量传递 32 位值,但每个分量的值仅限于 8 位。
  • ToString:将此 Color 结构转换为可读的字符串。

3、新建一个C#窗体项目,添加一个label控件、PictureBox控件和两个按钮。PictureBox控件SizeMode属性:AutoSize


4、代码:

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;

namespace showPicture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap image1;          				//定义Bitmap类实例
        private void button1_Click(object sender, EventArgs e)  	//打开方法
        {
            try
            {
                // Retrieve the image.
                image1 = new Bitmap(@"E:\abc.jpg", true);

                int x, y;

                // Loop through the images pixels to reset color.
                for (x = 0; x < image1.Width; 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);
                    }
                }

                // Set the PictureBox to display the image.
               pictureBox1.Image = image1;

               // Display the pixel format in Label1.
               label1.Text = "Pixel format: " + image1.PixelFormat.ToString();

            }
            catch (ArgumentException)
            {
                MessageBox.Show("There was an error." +"Check the path to the image file.");
            }
        }

        private void button2_Click(object sender, EventArgs e)  //保存方法
        {
            
        }
    }
}

5、运行结果


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值