c#单选按钮分配问题

c#单选按钮分配问题,c#,function,graphics,C#,Function,Graphics,我对c#中的单选按钮有一些问题。 我有一个叫做直方图的小类,里面有一个函数,可以绘制任何图片的直方图。我想把这个函数分配给一个单选按钮,这样当它被点击时,直方图就会被绘制出来。按钮8将把所有频道都画在一起,按钮9将只画红色频道,按钮10-绿色,按钮11-蓝色。这是我的表单设计图,也是我的直方图类代码。 需要分配给单选按钮的代码位于drawHistogram函数的末尾。我用箱子做了标记 using System; using System.Collections.Generic; using Sy

我对c#中的单选按钮有一些问题。 我有一个叫做直方图的小类,里面有一个函数,可以绘制任何图片的直方图。我想把这个函数分配给一个单选按钮,这样当它被点击时,直方图就会被绘制出来。按钮8将把所有频道都画在一起,按钮9将只画红色频道,按钮10-绿色,按钮11-蓝色。这是我的表单设计图,也是我的直方图类代码。 需要分配给单选按钮的代码位于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;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用系统、绘图、成像;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间Windows窗体应用程序4
{
公共类直方图
{
公共int[]人力资源;
公共int[]hG;
公共int[]hB;
公共int[]hI;
公共直方图()
{
hR=新整数[257];
hG=新整数[257];
hB=新整数[257];
hI=新整数[257];
}
~histogram()
{
hR=null;
hG=null;
hB=null;
hI=零;
}
公共图书馆
{

对于(int i=0;我与您提到的问题无关,但给您一个小小的建议,为什么不使用一些有意义的名称,例如
btnGreen
btnRed
等,来代替这种映射
按钮10-绿色,按钮11-蓝色
,那么问题是什么呢?还有,为什么您要使用终结器:它弊大于利。我想知道t单选按钮绘制我的直方图,直方图绘制代码位于drawHistogram函数的末尾除了添加一些
if(rb_red.Checked){..}
etc???之外,您还想要什么???