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、运行结果