是否有在一个图像中交换两种颜色的功能?

时间:2019-07-16 12:25:53

标签: c#

例如,我有一张带有白色背景和蓝色字的图片,我需要背景蓝色和白色字 像这样: https://docs.google.com/document/d/1-8s7FtjIF9lHQJzARk09Fy29emgkY9NjyxwHD9eiJoI/edit

2 个答案:

答案 0 :(得分:0)

我猜您正在创建Windows窗体应用程序? (.net)

如果是的话,这是一个如何选择和更改颜色的示例

public partial class Form1 : Form
{
    //add the folowing code to your form class

    private Bitmap bitmapImage; //load the image to this variable

    //private function
    void replaceColor(Color OriginalColor, Color DestColor)
    {
        for (int y = 0; y < bitmapImage.Height; y++)
        {
            for (int x = 0; x < bitmapImage.Width; x++)
            {
                   if(bitmapImage.GetPixel(x, y) == OriginalColor)
                   {
                        bitmapImage.SetPixel(x,y,DestColor);
                   }
            }
        }
    }
}

答案 1 :(得分:0)

我建议您重新映射表

在下面的示例中,我将红色更改为蓝色,并使用OnPaint事件来帮助我(在本例中为整个表单绘制)

    private void Form_Paint(object sender, PaintEventArgs e) 
    {
        string fileLocation = .....
        Graphics g = e.Graphics;
        using (Bitmap bmp = new Bitmap(fileLocation))
        {

            // Set the image attribute's color mappings
            ColorMap[] colorMap = new ColorMap[1];
            colorMap[0] = new ColorMap();
            colorMap[0].OldColor = Color.FromArgb(208, 33, 39); //red
            colorMap[0].NewColor = Color.Blue;
            using (ImageAttributes attr = new ImageAttributes())
            {
                attr.SetRemapTable(colorMap);
                // Draw using the color map
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, attr);
            }

        }
    }

示例

enter image description here