WPF GDI+画图
来源:互联网 发布:qq游戏hd登陆网络异常 编辑:程序博客网 时间:2024/05/02 00:33
文章内的代码主要是介绍了如何通过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;
}
}
0 0
- WPF GDI+画图
- gdi+ 画图
- GDI+画图
- GDI+画图
- WPF - 取代GDI/GDI+ ?
- WPF - 取代GDI/GDI+ ?
- WPF - 取代GDI/GDI+ ?
- WPF - 取代GDI/GDI+ ?
- gdi画图经验
- C# GDI+画图
- gdi画图基本步骤
- C# GDI画图
- GDI+抗锯齿画图
- gdi画图拉伸
- C# GDI+画图
- GDI+画图2
- GDI 画图问题集锦
- GDI+桌面画图
- Javascript获取select下拉框选中的的值
- Java面向对象
- 关于SpringMVC配置的问题
- startActivityForResult无法调用singleTask的Activity
- WebLogic优先加载Web应用程序中的jar包
- WPF GDI+画图
- Linux下蓝牙测试软件包和命令介绍
- log4cplus 和 log4j 配置和详解
- 思科智能云应用:白皮书与成功案例合集
- log4j入门
- ADF运行报错:ORA-01005: null password given; logon denied
- 用MySQL-Proxy实现读写分离
- vector进行erase以后迭代器失效问题
- ListView的使用和BaseAdater