private static int SortSpectrumByStartFreq(ChannelPowerSpectrum spectrum1, ChannelPowerSpectrum spectrum2)
 {
     return spectrum2.startFreq.CompareTo(spectrum1.startFreq);
 }
        private static bool drawCirclePicture(bool isVertical, string picturePath, 
            double azimuth, double azimuth_step, double sa_rbw,
            Dictionary<ChannelSetting, WatsEmiData> channelSamples,
            LimitSetting limitSetting)
        {
            try
            {
                Bitmap bmp = new Bitmap(300, 260);
                Graphics gs = Graphics.FromImage(bmp);
                Metafile mf = new Metafile(picturePath, gs.GetHdc());
                Graphics g = Graphics.FromImage(mf);
                using (Brush backgroundBrush = new SolidBrush(Color.White))
                {
                    g.FillRectangle(backgroundBrush, 0, 0, 300, 260);
                }

                using (Pen circlePen = new Pen(Color.Blue, 3.0f))
                {
                    g.DrawEllipse(circlePen, 45, 25, 210, 210);
                }

                ChannelPower power;
                ChannelPowerSpectrum spectrum;
                List<ChannelPowerSpectrum> spectrums = new List<ChannelPowerSpectrum>();
                foreach (KeyValuePair<ChannelSetting, WatsEmiData> pair in channelSamples)
                {
                    power = new ChannelPower(sa_rbw, pair.Key, limitSetting, pair.Value);
                    spectrum = new ChannelPowerSpectrum();
                    spectrum.startFreq = pair.Key.StartFreq;
                    spectrum.endFreq = pair.Key.EndFreq;
                    spectrum.isValidPower = (isVertical ? power.IsValidVPower : power.IsValidHPower);
                    spectrums.Add(spectrum);

                    power = new ChannelPower(sa_rbw, pair.Key.Pair, limitSetting, pair.Value);
                    spectrum = new ChannelPowerSpectrum();
                    spectrum.startFreq = pair.Key.Pair.StartFreq;
                    spectrum.endFreq = pair.Key.Pair.EndFreq;
                    spectrum.isValidPower = (isVertical ? power.IsValidVPairPower : power.IsValidHPairPower);
                    spectrums.Add(spectrum);
                }

                spectrums.Sort(SortSpectrumByStartFreq);
                float freqSpan = (float)(spectrums[0].endFreq - spectrums[spectrums.Count - 1].startFreq);
                int length;
                Rectangle rect;
                Color color;
                float drawAzimuth;
                for (int i = 0; i < spectrums.Count; i++)
                {
                    if (azimuth >= 0 && azimuth <= 90)
                        drawAzimuth = (float)azimuth - 90;
                    else if (azimuth >= 90 && azimuth <= 180)
                        drawAzimuth = (float)azimuth - 90;
                    else if (azimuth >= 180 && azimuth <= 270)
                        drawAzimuth =  (float)azimuth - 90;
                    else
                        drawAzimuth = (float)azimuth - 450;
                    Debug.WriteLine(spectrums[i].startFreq + " - " + spectrums[i].endFreq);
                    color = spectrums[i].isValidPower ? Color.Green : Color.Red;
                    using (Brush brush = new SolidBrush(color))
                    {
                        length = 2 * (int)(100 * (spectrums[i].endFreq - spectrums[spectrums.Count - 1].startFreq) / freqSpan);
                        rect = new Rectangle((300 - length) / 2, (260 - length) /2, length, length);
                        g.FillPie(brush, rect, (float)(drawAzimuth - azimuth_step / 2), (float)azimuth_step);
                    }

                    if (i != spectrums.Count - 1)
                    {
                        using (Brush whiteBrush = new SolidBrush(Color.White))
                        {
                            length = 2 * (int)(100 * (spectrums[i].startFreq - spectrums[spectrums.Count - 1].startFreq) / freqSpan);
                            rect = new Rectangle((300 - length) / 2, (260 - length) / 2, length, length);
                            g.FillPie(whiteBrush, rect, (float)(drawAzimuth - azimuth_step / 2), (float)azimuth_step);
                        }

                        using (Pen seperateCirclePen = new Pen(Color.White, 1.0f))
                        {
                            length = 2 * (int)(100 * (spectrums[i].endFreq - spectrums[spectrums.Count - 1].startFreq) / freqSpan);
                            rect = new Rectangle((300 - length) / 2, (260 - length) / 2, length, length);
                            g.DrawArc(seperateCirclePen, rect, (float)(drawAzimuth - azimuth_step / 2), (float)azimuth_step);
                        }
                    }
                }

                using (Pen directionPen = new Pen(Color.Blue, 2.0f))
                {
                    Point[] pt = new Point[]{new Point(50, 50), new Point(50, -50)};
                    GraphicsPath strokePath = new GraphicsPath();
                    strokePath.AddLine(new Point(0, 0), new Point(0, 5));
                    strokePath.AddLine(new Point(0, 5), new Point(3, 0));
                    strokePath.AddLine(new Point(0, 5), new Point(-3, 0));
                    CustomLineCap customLineCap = new CustomLineCap(null, strokePath);
                    customLineCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
                    directionPen.CustomEndCap = customLineCap;

                    Point center = new Point(150, 130);
                    int endX, endY;
                    int deltX = (int)(Math.Abs(Math.Sin(azimuth * Math.PI / 180)) * 120);
                    int deltY = (int)(Math.Abs(Math.Cos(azimuth * Math.PI / 180)) * 120);
                    if (azimuth >= 0 && azimuth <= 90)
                    {
                        endX = 150 + deltX;
                        endY = 130 - deltY;
                    }
                    else if (azimuth >= 90 && azimuth <= 180)
                    {
                        endX = 150 + deltX;
                        endY = 130 + deltY;
                    }
                    else if (azimuth >= 180 && azimuth <= 270)
                    {
                        endX = 150 - deltX;
                        endY = 130 + deltY;
                    }
                    else
                    {
                        endX = 150 - deltX;
                        endY = 130 - deltY;
                    }
                    Point end = new Point(endX, endY);
                    g.DrawLine(directionPen, center, end);

                    using (Font font = new Font("Times New Roman", 10.0f))
                    {
                        string text = azimuth.ToString() + "\x00B0";
                        SizeF sizef = g.MeasureString(text, font, Int32.MaxValue);
                        float textX, textY;
                        if (azimuth >= 0 && azimuth <= 90)
                        {
                            textX = end.X - 1;
                            textY = end.Y - sizef.Height - 10;
                        }
                        else if (azimuth >= 90 && azimuth <= 180)
                        {
                            textX = end.X - 1;
                            textY = end.Y + sizef.Height;
                        }
                        else if (azimuth >= 180 && azimuth <= 270)
                        {
                            textX = end.X - sizef.Width / 2;
                            textY = end.Y + sizef.Height;
                        }
                        else
                        {
                            textX = end.X - sizef.Width / 2;
                            textY = end.Y - sizef.Height - 10;
                        }

                        RectangleF rf = new RectangleF(textX, textY, sizef.Width, sizef.Height);
                        g.DrawString(text, font, Brushes.Blue, rf);
                    }
                }

                g.Save();
                g.Dispose();
            }
            catch (System.Exception e)
            {
                File.Delete("c:\\watsLog.txt");
                FileStream fs = new FileStream("c:\\watsLog.txt", FileMode.CreateNew);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(e.Message);
                sw.WriteLine(e.StackTrace);
                sw.Flush();
                sw.Close();
                fs.Close();

                return false;
            }
            return true;
        }