位图上的着色不准确(我已经研究过:如何在C#NET中更改图像的像素颜色)
我想做的是在一个事件发生后,我将所有的箭头图片设置为紫色 以下是我的箭头图片在绘制之前的样子: 以下是绘制后的外观: 首先,我用箭头列出所有图片框:位图上的着色不准确(我已经研究过:如何在C#NET中更改图像的像素颜色),c#,forms,colors,bitmap,C#,Forms,Colors,Bitmap,我想做的是在一个事件发生后,我将所有的箭头图片设置为紫色 以下是我的箭头图片在绘制之前的样子: 以下是绘制后的外观: 首先,我用箭头列出所有图片框: List<PictureBox> arrows = new List<PictureBox>(); foreach (var item in Controls.OfType<PictureBox>()) { if (item.Name.StartsWith("arr
List<PictureBox> arrows = new List<PictureBox>();
foreach (var item in Controls.OfType<PictureBox>())
{
if (item.Name.StartsWith("arrow"))
{
arrows.Add(item);
}
}
列表箭头=新列表();
foreach(Controls.OfType()中的变量项)
{
if(item.Name.StartsWith(“箭头”))
{
箭头。添加(项目);
}
}
这是我用来给图片上色的代码:
System.Drawing.Color purple = System.Drawing.Color.Purple;
foreach (var item in arrows)
{
Bitmap bmp = new Bitmap(item.Image, item.Height, item.Width);
for (int i = 0; i < item.Height; i++)
{
for (int j = 0; j < item.Width; j++)
{
var actualColor = bmp.GetPixel(i, j).ToArgb();
var purpleA = bmp.GetPixel(i, j).A;
if (actualColor != System.Drawing.Color.White.ToArgb())
{
bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(purpleA, purple));
} else
{
bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(actualColor));
}
}
}
item.Image = bmp;
}
System.Drawing.Color purple=System.Drawing.Color.purple;
foreach(箭头中的变量项)
{
位图bmp=新位图(item.Image、item.Height、item.Width);
对于(int i=0;i
如何准确地给图像上色?目前紫色的颜色真的很难看。我需要它是完全一样的黑色箭头,但不是黑色,我需要它是紫色的
注意:当我将黑色箭头图像放入picturebox时,我会调整其大小,因此在表单上,黑色箭头和紫色箭头的大小相同。我上传了带有截图的紫色箭头,黑色箭头来自我的电脑。这就是它们大小不同的原因。控件的尺寸可能与图像的尺寸不同 不要使用
item.Width
而使用item.Image.Width
等
例:
可以使用颜色贴图将一种颜色替换为另一种颜色。它比每个像素循环快得多
Graphics g = pictureBox.GetGraphics; // get the picture box graphics (i doubt this line compile but you get the idea of the object you need)
using (Bitmap bmp = new Bitmap("img.bmp")) // or the image currently in the picture box
{
// create the color map
var colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
// old color
colorMap[0].OldColor = Color.Black;
// replaced by this color
colorMap[0].NewColor = Color.Purple;
// attribute to remap the table of colors
var att = new ImageAttributes();
att.SetRemapTable(colorMap);
// draw result
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, att);
}
你说的精确是什么意思?真正的问题在哪里?我把照片贴了出来。看看紫色画得有多糟。我需要的箭头是完全相同的,但我需要它是紫色的,而不是黑色。阿尔法通道一定有问题。尝试将红色、绿色和蓝色部分与255进行比较,然后是白色。jpeg和png之间是否存在差异?我现在有了.png格式的图像。图像的背景应该是白色的。但是当我使用
if(actualColor!=255)
时,我会将整个图像涂成紫色。甚至是白色的部分。
Graphics g = pictureBox.GetGraphics; // get the picture box graphics (i doubt this line compile but you get the idea of the object you need)
using (Bitmap bmp = new Bitmap("img.bmp")) // or the image currently in the picture box
{
// create the color map
var colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
// old color
colorMap[0].OldColor = Color.Black;
// replaced by this color
colorMap[0].NewColor = Color.Purple;
// attribute to remap the table of colors
var att = new ImageAttributes();
att.SetRemapTable(colorMap);
// draw result
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, att);
}