代码之家  ›  专栏  ›  技术社区  ›  Carlo

WPF中的RGB游行

  •  1
  • Carlo  · 技术社区  · 14 年前

    我应该在WPF中创建这个RBG游行项目,但我不知道从哪里开始。它应该处理720p设置(1280x720)的图像。我完全不知道从哪里开始。我不希望整个项目只是一些提示。

    谢谢您。

    1 回复  |  直到 11 年前
        1
  •  0
  •   ChrisF    14 年前

    好吧,这就是我想出来的,我相信有更好的解决方案,但它是有效的:

    public ObservableCollection<double> RPercentPerColumn { get; set; }
    public ObservableCollection<double> GPercentPerColumn { get; set; }
    public ObservableCollection<double> BPercentPerColumn { get; set; }
    
    private int[] GetImagePixels(ref int imageHeight, ref int imageWidth)
    {
        BitmapImage image = new BitmapImage(new Uri(this.CurrentImageFile, UriKind.Absolute));
    
        WriteableBitmap bmp = new WriteableBitmap(image);
    
        int rows = bmp.PixelHeight;
        int columns = bmp.PixelWidth;
    
        int[] pixels = new int[bmp.PixelWidth * bmp.PixelHeight];
    
        bmp.CopyPixels(pixels, columns * 4, 0);
    
        imageHeight = bmp.PixelHeight;
        imageWidth = bmp.PixelWidth;
    
        return pixels;
    }
    
    private void ClearAllData()
    {
        this.RPercentPerColumn.Clear();
        this.GPercentPerColumn.Clear();
        this.BPercentPerColumn.Clear();
    }
    
    public void ProcessRGBParade()
    {
        if (string.IsNullOrEmpty(this.CurrentImageFile))
            return;
    
        ClearAllData();
    
        int columns = 0;
        int rows = 0;
    
        int[] pixels = GetImagePixels(ref rows, ref columns);
    
        int column = 0;
        int row = 0;
    
        int redColumnTotal = 0;
        int greenColumnTotal = 0;
        int blueColumnTotal = 0;
        int currentPixel = 0;
    
        double totalColorInColumn = 0;
    
        double redIntensity = 0;
        double greenIntensity = 0;
        double blueIntensity = 0;
    
        int r = 0;
        int g = 0;
        int b = 0;
    
        // logic to calculate intensity
        for (int i = 0; i < pixels.Length; i ++)
        {
            row++;
    
            r = (pixels[currentPixel] & 0x00FF0000) >> 16;
            g = (pixels[currentPixel] & 0x0000FF00) >> 8;
            b = (pixels[currentPixel] & 0x000000FF);
    
            redColumnTotal += r;
            greenColumnTotal += g;
            blueColumnTotal += b;
    
            totalColorInColumn += r + g + b;
    
            if (row == rows)
            {
                row = 0;
                column++;
    
                currentPixel = column;
    
                redIntensity = (redColumnTotal / totalColorInColumn) * 100;
                greenIntensity = (greenColumnTotal / totalColorInColumn) * 100;
                blueIntensity = (blueColumnTotal / totalColorInColumn) * 100;
    
                RPercentPerColumn.Add(double.IsNaN(redIntensity) ? 0 : redIntensity);
                GPercentPerColumn.Add(double.IsNaN(greenIntensity) ? 0 : greenIntensity);
                BPercentPerColumn.Add(double.IsNaN(blueIntensity) ? 0 : blueIntensity);
    
                redColumnTotal = 0;
                greenColumnTotal = 0;
                blueColumnTotal = 0;
    
                totalColorInColumn = 0;
            }
            else
            {
                currentPixel += columns;
            }
        }
    }