我现在要做的就是把一张手绘的线性图片(黑白的)给添加上颜色,线条也要求添加上颜色,请高手帮忙(最好能给 我一个例子)
解决方案 »
- 关于DES 过程密钥和密钥分散
- C# 模拟网吧管理系统
- 如何使用XPath查询Xml?
- float类型数据问题:带小数的数字相加后,小数位丢失
- 无法实时刷新
- 如何在程序中创建windwos服务?
- @@@@@@@@@###############快来看看,这样能否实现,老出未将对象引用设置到对象的实例问题..........
- 急救——Microsoft Visual Studio遇到了问题,需要关闭错误!
- 我的前台的控件在后台说找不到呢
- 关于数据库的问题:100分求援!(MS Access)
- 求 C#版本,Winform(即windows窗口)程序开发 视频教程!! 不是asp.net的!不是web的!
- 关于窗体之间调用的一个问题
可以循环GetPixel获取像素颜色,这个方法有点慢。
使用ColorMap和ImageAttributes替换旧的颜色
ColorMap[] remapTable; //颜色转换列表
g.DrawImage(
img,
destBounds, // destination rectangle
0, 0, // upper-left corner of source rectangle
bmp.Width, // width of source rectangle
bmp.Height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
g.DrawImage(
img,
destBounds, // destination rectangle
0, 0, // upper-left corner of source rectangle
bmp.Width, // width of source rectangle
bmp.Height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);这个方法我试过,这个只能对对图片整体设置透明度,不是我想要的效果。我的想法是先给图片设置一个背景色,然后对不同的颜色设置不同的透明度,比如白色的设置透明度为完全不透明,黑色的就设置完全透明的,这样应该就能实现,可是这个透明度我咋改都不好使
/// <summary>
/// 替换图片中的指定颜色为其它颜色
/// </summary>
class ImageMaker
{
ImageAttributes imageAttributes = new ImageAttributes();
List<ColorMap> mapTable = new List<ColorMap>(); //颜色替换列表
//添加颜色映射
public void AddRemap(Color oldColor, Color newColor)
{
ColorMap colorMap = new ColorMap();
colorMap.OldColor = oldColor;
colorMap.NewColor = newColor;
mapTable.Add(colorMap);
} private Image img;
public Image Img
{
get { return img; }
set { img = value; }
} //根据颜色替换列表对img进行颜色替换
public Bitmap Replace()
{
if (mapTable.Count < 1)
{
return new Bitmap(img);
}
imageAttributes.SetRemapTable(mapTable.ToArray(), ColorAdjustType.Bitmap);
Bitmap bmp = new Bitmap(img.Width, img.Height);
Rectangle destBounds = new Rectangle(Point.Empty, img.Size);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(
img,
destBounds, // destination rectangle
0, 0, // upper-left corner of source rectangle
bmp.Width, // width of source rectangle
bmp.Height, // height of source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
return bmp;
} }
private void button1_Click(object sender, EventArgs e)
{
ImageMaker imgMaker = new ImageMaker();
imgMaker.Img = pictureBox1.Image;
Color nc = Color.FromArgb(0, Color.Black);
imgMaker.AddRemap(Color.Black, nc);//黑色透明 nc = Color.Blue;
imgMaker.AddRemap(Color.White, nc); //白色变蓝色 Bitmap bitmap = imgMaker.Replace();
pictureBox1.Image = bitmap;
}可以实现多种颜色的替换,可能你的用法不对吧。