WPF GDI+画图
2015-04-13 16:19
183 查看
文章内的代码主要是介绍了如何通过GDI+的方式在WPF中进行画图。
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private System.Drawing.Image bitmap = null;
private MemoryStream mos = null;
private int lastX = -1;
private double lastY = -1;
Thread t = null;
public MainWindow()
{
mos = new MemoryStream();
InitializeComponent();
bitmap = new Bitmap(800,600) ;
t = new Thread(CreateDataAndDraw);
t.IsBackground = true;
t.Start();
this.img.MouseDown += img_MouseDown;
this.img.MouseUp += img_MouseUp;
}
void img_MouseUp(object sender, MouseButtonEventArgs e)
{
locker.Set();
}
void img_MouseDown(object sender, MouseButtonEventArgs e)
{
locker.Reset();
}
public static ManualResetEvent locker = new ManualResetEvent(true);
public void CreateDataAndDraw()
{
while (true)
{
Thread.Sleep(10);
locker.WaitOne();
double y = new Random().NextDouble() * 200 + 200;
this.Dispatcher.Invoke(() =>
{
DrawTest(lastX+5, y);
});
lastX += 5;
lastY = y;
locker.Set();
}
}
public void DrawTest(int pointX, double pointY)
{
Graphics g = Graphics.FromImage(bitmap);
g.DrawLine(Pens.AliceBlue, new PointF(lastX, (float)lastY), new PointF(pointX, (float)pointY));
g.Save();
bitmap.Save(mos, ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = mos;
bitmapImage.EndInit();
img.Source = bitmapImage;
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private System.Drawing.Image bitmap = null;
private MemoryStream mos = null;
private int lastX = -1;
private double lastY = -1;
Thread t = null;
public MainWindow()
{
mos = new MemoryStream();
InitializeComponent();
bitmap = new Bitmap(800,600) ;
t = new Thread(CreateDataAndDraw);
t.IsBackground = true;
t.Start();
this.img.MouseDown += img_MouseDown;
this.img.MouseUp += img_MouseUp;
}
void img_MouseUp(object sender, MouseButtonEventArgs e)
{
locker.Set();
}
void img_MouseDown(object sender, MouseButtonEventArgs e)
{
locker.Reset();
}
public static ManualResetEvent locker = new ManualResetEvent(true);
public void CreateDataAndDraw()
{
while (true)
{
Thread.Sleep(10);
locker.WaitOne();
double y = new Random().NextDouble() * 200 + 200;
this.Dispatcher.Invoke(() =>
{
DrawTest(lastX+5, y);
});
lastX += 5;
lastY = y;
locker.Set();
}
}
public void DrawTest(int pointX, double pointY)
{
Graphics g = Graphics.FromImage(bitmap);
g.DrawLine(Pens.AliceBlue, new PointF(lastX, (float)lastY), new PointF(pointX, (float)pointY));
g.Save();
bitmap.Save(mos, ImageFormat.Bmp);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = mos;
bitmapImage.EndInit();
img.Source = bitmapImage;
}
}
相关文章推荐
- win32 GDI 画图 防止闪烁
- c# image GDI+发生一般性错误 & c# wpf use winform‘s Image
- WPF - GDI/GDI+的末路, Direct3D的盛宴
- 未来的GDI:WPF技术纵览[zz]
- wpf 使用路径画图
- SDK一个简单的GDI程序(就是画图)
- gdi画图经验
- WPF InkCanvas 画图 基础使用教程
- win32 GDI 画图
- 提升GDI画图的效率
- 关于WPF画图性能问题
- WPF画图性能问题
- WPF - GDI/GDI+的末路, Direct3D的盛宴[zz]
- GDI画图,判断鼠标点击点在某一画好的多边形、矩形、图形里
- 如何解决在GDI画图中,多次修改画笔的颜色
- WPF的画图请求是如何生成和派发
- WPF - 取代GDI/GDI+ ?
- WPF学习笔记--运行时画图的实现
- WPF优点之比较GDI,GDI+
- C#GDI+ winform简单画图(1)