实现图象颜色反转这一简单效果可以使用ColorMatrix轻松实现,下面介绍这一实现过程!
第一步:打开原始图象
第二步:在内存中创建一个和原始图象同样尺寸的图象
第三步:创建一个转化矩阵,并建立其与ImageAttributes对象的联系
第四步:创建一个Graphics对象
第五步:利用Graphics对象的DrawImage方法将原始图象使用颜色矩阵绘制到在内存中的那个图象,以实现反转效果.
下面是我做的一个实验代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace 用颜色矩阵类实现图象颜色的反转效果
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog
openFileDialog1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何
构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>