这是原始图像Colorslevels1.jpg:

colors levels

Original Image

然后,我使用这两种方法从图像中获取特定 colored颜色 ,并将这些 colored颜色 添加到新图像中.

private List<Color> GetColors()
{
    if (bmp != null)
    {
        List<string> targetColors = new List<string>
{
    "#ff00fe",
    "#d00078",
    "#e10a12",
    "#fb3f00",
    "#ff7d01",
    "#ffa800",
    "#ffcf00",
    "#fefd19",
    "#7cff21",
    "#12f218",
    "#01d31c",
    "#00b02c",
    "#008c4d",
    "#00a09b",
    "#00c3c9",
    "#0165ef"
};

        bmp = new Bitmap(@"d:\Ffmpeg Capture\colorslevels1.jpg");

        int width = bmp.Width;
        int height = bmp.Height;

        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                Color c = bmp.GetPixel(i, j);
                string hexColor = ColorTranslator.ToHtml(c).ToLower(); // Convert to lowercase

                // Check if the lowercase hex color matches any of the target colors
                if (targetColors.Contains(hexColor))
                {
                    colors.Add(c);
                }
            }
        }

        bmp.Dispose();

        // Save the bitmap with the colors once all colors are collected
        SaveColorsAsBitmap(colors, @"d:\test.bmp");
    }

    return colors;
}

此方法将十六进制 colored颜色 保存为位图到硬盘.

private void SaveColorsAsBitmap(List<Color> colors, string filePath)
{
    // Determine the dimensions of the bitmap
    int width = 200; // Set the desired width (adjust as needed)
    int height = colors.Count; // Set the height to the number of colors

    // Create a new transparent bitmap with the determined dimensions
    Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

    using (Graphics g = Graphics.FromImage(bitmap))
    {
        // Clear the background to transparent
        g.Clear(Color.Transparent);

        // Draw each color as a vertical line on the bitmap
        for (int i = 0; i < height; i++)
        {
            using (SolidBrush brush = new SolidBrush(colors[i]))
            {
                g.FillRectangle(brush, 0, i, width, 1);
            }
        }
    }

    // Save the bitmap as a PNG file (PNG supports transparency)
    bitmap.Save(filePath, ImageFormat.Png);

    // Dispose of the bitmap to release resources
    bitmap.Dispose();
}

在方法SaveColorsAsBitmap中,我将宽度值设置为200,因为这样会太小,无法看到新位图图像中的 colored颜色 .在原始版本中,宽度值为1.

结果是:

result

Result Image

在结果中,新图像具有我想要的 colored颜色 ,但每种 colored颜色 的宽度/高度和 colored颜色 数量与原始图像中的不同.这是我想要从原始图像中获得的顺序和 colored颜色 ,但每种 colored颜色 的大小(宽度/高度)与原始图像不同.

我如何修复它才能得到原始图像中的 colored颜色 ?

推荐答案

您的代码中有几个问题:

  1. 如果在分析源图像之后、调用SaveColorsAsBitmap之前查看"Colors"数组的内容,则会发现值太多且重复.我认为数组中的元素数量应该与原始图像中的 colored颜色 数量相匹配.首先,在将一种 colored颜色 添加到"Colors"数组之前,您需要判断该数组中是否已经存在这样的 colored颜色 .要做到这一点,您可以稍微更改"Colors"数组的 struct ,即创建一个将包含 colored颜色 值及其十六进制表示的类.在此之后,数组将包含原始图像中的 colored颜色 数量.
  2. 在SaveColorsAsBitmap方法中,需要设置将用color-widthColorBlock和heightColorBlock填充的矩形的大小.此外,在图形中绘制时,必须遵循沿Y轴的步长,即后续的每种 colored颜色 都必须低于另一种 colored颜色 .

Result image

附注:不要忘记在代码中更改源和结果图像的路径

完整代码:

public class ColorItem
{
    public Color color { get; set; }
    public string hexColor { get; set; }
}

private List<ColorItem> GetColors()
{
    List<string> targetColors = new List<string>
    {
        "#ff00fe",
        "#d00078",
        "#e10a12",
        "#fb3f00",
        "#ff7d01",
        "#ffa800",
        "#ffcf00",
        "#fefd19",
        "#7cff21",
        "#12f218",
        "#01d31c",
        "#00b02c",
        "#008c4d",
        "#00a09b",
        "#00c3c9",
        "#0165ef"
    };

    List<ColorItem> colors = new List<ColorItem>();

    using (var bmp = new Bitmap(@"sourceImage.jpg"))
    {
        int width = bmp.Width;
        int height = bmp.Height;

        for (int j = 0; j < height; j++)
        {
            for (int i = 0; i < width; i++)
            {
                Color c = bmp.GetPixel(i, j);
                string hexColor = ColorTranslator.ToHtml(c).ToLower(); // Convert to lowercase

                // Check if the lowercase hex color matches any of the target colors
                if (targetColors.Contains(hexColor))
                {
                    if (colors.Count(d => d.hexColor == hexColor) == 0)
                    {
                        colors.Add(new ColorItem() { color = c, hexColor = hexColor });
                    }
                }
            }
        }

        // Save the bitmap with the colors once all colors are collected
        SaveColorsAsBitmap(colors, @"resultImage.png");
    }

    return colors;
}

private void SaveColorsAsBitmap(List<ColorItem> colors, string filePath)
{
    int widthColorBlock = 39; // color rectangle width
    int heightColorBlock = 18; // color rectangle height

    int imageHeight = 377; //result image height
    int imageWidth = 106;  //result image width

    var startDrawColorsPoint = new Point(45, 38); // start point (x,y) for drawing colors

    var hexBackground = "#CECECE"; 
    var backgroundColor = System.Drawing.ColorTranslator.FromHtml(hexBackground); // result image background color and hex color value

    var borderThickness = 1;  // thickness of lines around color (pixel)

    using (Bitmap bitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format32bppArgb))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.Clear(backgroundColor);

            for (int i = 0; i < colors.Count; i++)
            {
                using (SolidBrush brush = new SolidBrush(colors[i].color))
                {
                    //draw black border
                    g.FillRectangle(Brushes.Black, startDrawColorsPoint.X, (heightColorBlock * i) + startDrawColorsPoint.Y, widthColorBlock, heightColorBlock + borderThickness);

                    //draw color block
                    var colorBlockPositionX = startDrawColorsPoint.X + borderThickness;
                    var colorBlockPositionY = startDrawColorsPoint.Y + (heightColorBlock * i) + borderThickness;
                    var colorBlockWidth = widthColorBlock - (borderThickness * 2);
                    var colorBlockHeight = heightColorBlock - borderThickness;

                    g.FillRectangle(brush, colorBlockPositionX, colorBlockPositionY, colorBlockWidth, colorBlockHeight);
                }
            }
        }

        // Save the bitmap as a PNG file (PNG supports transparency)
        bitmap.Save(filePath, ImageFormat.Png);
    }
}

Csharp相关问答推荐

List T.AddRange在传递ConcurrentDictionary作为参数时引发ArgumentExcellent

如何使用CsvReader获取给定列索引的列标题?

为什么在ANTLR4中会出现不匹配的输入错误?""

实体核心框架--HasColumnType和HasPrecision有什么不同?

Amazon SP-API确认发货不设置&Quot;递送服务

TCPClient阅读流

在C#中,将两个哈希集连接在一起的时间复杂度是多少?

为什么@rendermode Interactive Auto不能在.NET 8.0 Blazor中运行?

无法使用[FromForm]发送带有图像和JSON的多部分请求

在PostgreSQL上使用ExecuteSqlRawAsync的C#11原始字符串文字有区分大小写的问题

为什么我的伺服电机不动,下面的代码?

使用可空引用类型时C#接口实现错误

FakeItEasy自动嘲弄内容

WPF:如何从DatagridHeader的内容模板绑定到词典项

使用C#12中的主构造函数进行空判断

Azure队列触发器未使用隔离的工作进程执行

我是否以错误的方式使用了异步延迟初始化?

当我在Git中暂存文件更改时,它们会消失

是否在异步方法中避免Span<;T>;.ToArray()?

ASP.NET重新加载Kestrel SSL证书