c#单选按钮分配问题

时间:2017-03-02 02:28:14

标签: c# function graphics

我在c#中的单选按钮有一些问题。 我有一个叫做直方图的小类,里面有一个绘制任何图片直方图的函数。我想将该功能分配给单选按钮,以便在单击时可以绘制直方图。按钮8将所有通道绘制在一起,按钮9将仅绘制红色通道,按钮10 - 绿色,按钮11 - 蓝色。这是我的表单设计和直方图类代码的图片。 http://i.imgur.com/x9Hd0TL.png需要分配给单选按钮的代码位于drawHistogram函数的末尾。我用案件标记了他们。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication4
{
    public class histogram
    {
        public int[] hR;
        public int[] hG;
        public int[] hB;
        public int[] hI;

    public histogram()
    {
        hR = new int[257];
        hG = new int[257];
        hB = new int[257];
        hI = new int[257];
    }

    ~histogram()
    {
        hR = null;
        hG = null;
        hB = null;
        hI = null;
    }

    public void eraseHistogram()
    {
        for (int i = 0; i <257; i++)
        {
            hR[i] = 0;
            hG[i] = 0;
            hB[i] = 0;
            hI[i] = 0;
        }
    }

    public void readHistogram(rgbiPixels[,] imgArray)
    {
        eraseHistogram();
        int W = imgArray.GetLength(0);
        int H = imgArray.GetLength(1);

        for (int x = 0; x < W; x++)
        {
            for (int y = 0; y < H; y++)
            {
                hR[imgArray[x, y].R]++;
                hR[256] = Math.Max(hR[imgArray[x, y].R], hR[256]);
                hI[imgArray[x, y].I]++;
                hI[256] = Math.Max(hI[imgArray[x, y].I], hI[256]);
                hG[imgArray[x, y].G]++;
                hG[256] = Math.Max(hG[imgArray[x, y].G], hR[256]);
                hB[imgArray[x, y].B]++;
                hB[256] = Math.Max(hB[imgArray[x, y].B], hR[256]);
            }
        }
    }

    public Bitmap drawHistogram()
    {
        int r;
        int g;
        int b;
        int i;

        int normalizedMax = Math.Max(hI[256], Math.Max(hR[256], Math.Max(hG[256], hB[256])));

        var bmp = new Bitmap(256, 100, PixelFormat.Format24bppRgb);

        for (int x = 0; x < 256; x++)
        {
            for (int y = 0; y < 100; y++)
            {
                int normalizedValue = 100 * hI[x] / normalizedMax;

                if (y < normalizedValue) { i = 255; }
                else { i = 0; }

                normalizedValue = 100 * hR[x] / normalizedMax;

                if (y < normalizedValue) { r = 255; }
                else { r = 0; }

                normalizedValue = 100 * hG[x] / normalizedMax;

                if (y < normalizedValue) { g = 255; }
                else { g = 0; }

                normalizedValue = 100 * hB[x] / normalizedMax;

                if (y < normalizedValue) { b = 255; }
                else { b = 0; }

                //case 1, intensity
                if (r == 0 && g == 0 && b == 0)
                { bmp.SetPixel(x, 99 - y, Color.FromArgb(i, i, i)); }
                else if (i == 255)
                { bmp.SetPixel(x, 99 - y, Color.FromArgb((i + r) / 2, (i + g) / 2, (i + b) / 2)); }
                else
                { bmp.SetPixel(x, 99 - y, Color.FromArgb(r, g, b)); }

                /*//case 2, red
                if (r == 255)
                { bmp.SetPixel(x, 99 - y, Color.FromArgb(255, 0, 0)); }*/

                /*//case 3, green
                if (g == 255)
                { bmp.SetPixel(x, 99 - y, Color.FromArgb(0, 255, 0)); }*/

                /*//case 4, blue
                if (b == 255)
                { bmp.SetPixel(x, 99 - y, Color.FromArgb(0, 0, 255)); }*/
            }
        }
        return bmp;
    }
}

}

0 个答案:

没有答案