AForge学习笔记(2):AForge.Controls

来源:互联网 发布:淘宝大学免费课程视频 编辑:程序博客网 时间:2024/05/16 13:07

首先我们在VS2010中添加AForge.Controls提供的控件:

        

    Chart:这一控件提供了AddDataSeries,Dispose,RemoveAllDataSeries,RemoveDataSeries,UpdataDataSeries方法,通过下面的简单实例我们进行学习:

     实例:// create data series array
               double[,] testValues = new double[10, 2];
               // fill data series
               for ( int i = 0; i < 10; i++ )
               {
                   testValues[i, 0] = i; // X values
                   testValues[i, 1] = Math.Sin( i *i ); // Y values
               }
              // add new data series to the chart
                 chart.AddDataSeries( "Test", Color.DarkGreen, Chart.SeriesType.ConnectedDots, 3 );

             // set X range to display
                 chart.RangeX = new AForge.Range( 0, 9 );
             // update the chart
                 chart.UpdateDataSeries( "Test", testValues );

      运行结果:

      

    ColorSlider:这一控件包含有ColorSliderType枚举类型,类型有:Gradient,InnerGradient,OuterGradient,Threshold。使用见实例:

     实例:colorSlider1.DoubleArrow = true;//使用双向箭头
                 colorSlider1.EndColor = Color.Red;//以红色结束
                 colorSlider1.FillColor = Color.Orange;//控件背景色
                 colorSlider1.Min = 10;//设定【0,255】区间最小值                
                 colorSlider1.Max = 200;//设定【0,255】区间最大值
                 colorSlider1.StartColor = Color.Blue;//起始颜色设置
                 colorSlider1.Type = ColorSlider.ColorSliderType.Gradient;//设定颜色渐变类型

    Histogram:为Value赋整型值,即可获得直方图,示例如下:

    实例://create array with histogram values
               int[] histogramValues = new int[500];
               Random rd = new Random();
               AForge.Parallel.For(0,500,delegate(int i)
               {
                   //为直方图赋值,值为随机数产生   
                   histogramValues[i]=rd.Next(100);
               });
               histogram.AllowSelection = true;//允许鼠标选择
               histogram.Color = Color.Blue;//直方图颜色

               histogram.IsLogarithmicView = true;//显示值以对数为底
               histogram.IsVertical = false;//直方图是否垂直显示
               // set values to histogram control
               histogram.Values = histogramValues;

     运行结果:

      

      HuePicker:该控件用于选择色彩,色彩值为整型,位于【0,359】,包括Value,Range两种类型,示例如下:

      实例: huePicker1.Max = 350;//指定色彩最大值
                  huePicker1.Min = 10;//指定色彩最小值
                  huePicker1.Type = AForge.Controls.HuePicker.HuePickerType.Range;//指定色彩选择类型

                  //HuePicker的ValuesChanged事件
                  private void huePicker1_ValuesChanged(object sender, EventArgs e)
                  {
                       textBox1.Text = huePicker1.Value.ToString();
                   }

     运行结果:

      

     ManipulatorControl :这一控件是为了模拟控制器,通过改变中心点的相对位置,产生被控制体位置以及力度的变化。它有一个重要属性:ResetPositionOnMouseRelease,用以确定控制器行为,通常我们在PositionChanged事件中进行操纵器控制,示例如下:

     实例://重置窗体大小
                 Size size = new System.Drawing.Size(2 * manipulatorControl1.Width,manipulatorControl1.Height);
                 this.Size = size;

                 //Manipulator色彩设置
                manipulatorControl1.BorderColor = Color.Blue;
                manipulatorControl1.BottomLeftBackgroundColor = Color.Brown;
                manipulatorControl1.BottomRightBackgroundColor = Color.CadetBlue;
                manipulatorControl1.DrawHorizontalAxis = true;
                manipulatorControl1.DrawVerticalAxis = true;
                manipulatorControl1.ManipulatorColor = Color.Coral;
                manipulatorControl1.TopLeftBackgroundColor = Color.DarkBlue;
                manipulatorControl1.TopRightBackgroundColor = Color.DarkGreen;
                //确定是否位置移动是否进行行为操作
                manipulatorControl1.ResetPositionOnMouseRelease = false;
                bt=new Bitmap(@"C:\Users\GAOXIANG\Desktop\dog.jpg");
                gp = this.CreateGraphics();//通过窗体创建GDI+
                //设置图片初始位置
                x = 3 * manipulatorControl1.Width / 2-bt.Width/2;
                y = manipulatorControl1.Height / 2-bt.Height/2;

               //记录操控器当前位置
              private void manipulatorControl1_PositionChanged(object sender, ManipulatorControl.PositionEventArgs me)
              {
                  a = me.X;
                  b = me.Y;
              }

             //操控器位置改变改变当前对象,实现操控
             private void manipulatorControl1_MouseUp(object sender, MouseEventArgs e)
             {
                 this.Refresh();
                 if (e.Button == MouseButtons.Left)
                 {
                     gp.DrawImage(bt, x + a * (manipulatorControl1.Width / 2), y - b * (manipulatorControl1.Height / 2));
                 }
             }

     运行结果:

        

    PictureBox:在AForge中该控件继承于Form中的PictureBox,但是该控件解决了Form中PictureBox不能显示16bpp,48bpp,64bpp彩色图像。

   SliderControl:滑动条,与Manipulator控件使用方法类似,在次不再讨论。

    VideoSourcePlayer:用以支持视频播放,这一控件的使用我们在AForge.Video中探讨。

原创粉丝点击