【问题标题】:Coloring on bitmap not accurate (i already looked at: How to Change Pixel Color of an Image in C#.NET)位图上的着色不准确(我已经看过:如何在 C#.NET 中更改图像的像素颜色)
【发布时间】:2017-03-10 03:28:05
【问题描述】:

我要做的是在事件发生后将所有箭头图片设置为紫色。

这是我的箭头图片在绘制之前的样子:

绘制后的样子如下:

首先我用箭头列出所有图片框:

 List<PictureBox> arrows = new List<PictureBox>();
 foreach (var item in Controls.OfType<PictureBox>())
        {
            if (item.Name.StartsWith("arrow"))
            {
                arrows.Add(item);
            }    
        }

这是我用来为图片着色的代码:

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;
        }

如何准确地为图像着色?目前紫色画得很差。我需要它与黑色箭头完全相同,但我需要它是紫色而不是黑色。

注意:我将黑色箭头图像放入图片框时调整了它的大小,因此在表格中黑色和紫色箭头的大小相同。我上传了带有屏幕截图的紫色箭头,黑色箭头来自我的电脑。这就是它们大小不同的原因。

【问题讨论】:

  • 你到底是什么意思?真正的问题在哪里?
  • 我发布了图片。看看紫色画得有多糟糕。我需要箭头完全相同,但我需要的是紫色而不是黑色。
  • Alpha 通道一定有问题。尝试将红色绿色和蓝色部分与255进行比较,则为白色。
  • jpeg 和 png 有区别吗?我现在将图像保存为 .png。图片的背景应该是白色的。但是当我使用if (actualColor != 255) 时,我会将整个图像染成紫色。甚至是白色的部分。

标签: c# forms colors bitmap


【解决方案1】:

您可以使用颜色图将一种颜色替换为另一种颜色。它比循环每个像素要快得多。

    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);
    }

【讨论】:

    【解决方案2】:

    控件的尺寸可能与图像的不同。

    使用item.Image.Width 等代替item.Width

    前:

    var itemImage = item.Image;
    Bitmap bmp = new Bitmap(itemImage, itemImage.Height, itemImage.Width);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多