怎样绘制一个波形,把从串口接收的数据 绘制成波形?我现在的方法的 实例化一个位图对象 在位图对象上绘制好以后 
再显示。现在碰到了个问题 我不知怎样绘制在位图对象上? 求高手给点代码或给个其他的方法。

解决方案 »

  1.   

    直接贴代码,
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Image Height="200" HorizontalAlignment="Left" Margin="24,24,0,0" Name="image1"  VerticalAlignment="Top" Width="300" Loaded="image1_Loaded" />
        </Grid>
    </Window>using System;
    using System.Windows;
    using System.Windows.Media.Imaging;
    using System.Windows.Interop;
    using System.Drawing;
    using System.Drawing.Drawing2D;namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }        private void image1_Loaded(object sender, RoutedEventArgs e)
            {
                Bitmap bitmap = CreateSineWave();
                image1.Source = Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }        private Bitmap CreateSineWave()
            {
                const int delta = 20;
                var bitmap = new Bitmap(300, 200);
                using (var g = Graphics.FromImage(bitmap))
                {
                    var path = new GraphicsPath();
                    for (double a = 0; a < 4 * Math.PI; a += 0.2)
                    {
                        double x1 = a * delta;
                        double y1 = Math.Sin(a) * delta + 100;
                        double x2 = (a + 0.1) * delta;
                        double y2 = Math.Sin(a + 0.1) * delta + 100;                    path.AddLine((float)x1, (float)y1, (float)x2, (float)y2);
                    }
                    g.DrawPath(new Pen(Color.Black), path);
                }            return bitmap;
            }
        }
    }