日期:2014-05-17  浏览次数:21274 次

c# 数字图像处理直方图绘制
照着赵春江那本书写的代码~ 代码和下面从网上copy来的一致~但是我的显示为什么是这样的哇?恳请各位高手解决~


双击“histogram”控件,并在其中添加如下代码:
       private void histogram_Click(object sender, EventArgs e)
        {
            if (curBitmap != null)
            {
                //定义并实例化新窗体,并把图像数据传递给它;
                histForm histoGram = new histForm(curBitmap);
                //打开从窗体;
                histoGram.ShowDialog();
            }
            else
            {
                //没有打开图像时,弹出对应的提示框;
                MessageBox.Show("请先打开图像!");
            }
        }
 
然后在打开的histForm窗体中先定义以下3个数据成员:
        //图像数据
        private System.Drawing.Bitmap bmpHist;
        //为了标识横纵坐标的标识;
        private int[] countPixel;
        
        private int maxPixel=1000;//记录最大的灰度级个数
为了实现两个窗体之间的数据传递,需要改写histForm窗体的构造函数,其代码如下:
       public histForm(Bitmap bmp)
        {
            InitializeComponent();
            //把主窗体的图像数据传递给从窗体
            bmpHist = bmp;
            //灰度级计数
            countPixel=new int[256];
        }
 
分别为histForm窗体添加Paint和Load事件,Paint事件用于绘制直方图,Load事件用于计算各个灰度级所具有的像素个数,其代码如下:
                                                              
1——————————————————————Paint事件;
        private void histForm_Paint(object sender, PaintEventArgs e)
        {
            //获取Graphics对象
            Graphics g = e.Graphics;

            //创建一个宽度为1的黑色钢笔
     &nb