111,092
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reactive.Linq; //这是Rx类,你可以用nuget管理器搜索Reactive Extensions就可以搜索到,大胆使用把这是微软的东西,不是其他人滴提供
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
IDisposable _mouseMoveSubscribe;
Image _sourceImage;
Image _tempImage;
private void Form1_Load(object sender, EventArgs e)
{
_sourceImage = Image.FromFile(@"I:\新建文件夹1\000001_20151209_00097051.BMP");
_tempImage = Image.FromFile(@"I:\新建文件夹1\000001_20151209_00097051.BMP");
this.pictureBox1.Image = _sourceImage;
this.WindowState = FormWindowState.Maximized;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Point startPoint = e.Location;
//鼠标按下左键开启,rx订阅鼠标事件的可观测流
_mouseMoveSubscribe =
Observable.FromEventPattern<MouseEventArgs>(this.pictureBox1, "MouseMove") //将传统事件数据转换成可观察流
.Buffer(2) //按你的需要缓存鼠标移动数据事件,每两次推送一次
.Subscribe(x =>
{
MemoryStream ms = new MemoryStream();
_tempImage.Save(ms, ImageFormat.Bmp);
Image t = Image.FromStream(ms);
Graphics graph = Graphics.FromImage(t);
//上下代码都不用管,那是绘图代码。主要讲这里,因为是2次缓存的数据流,我为了演示你的需要,特地写成求2次鼠标位置的平均算术值
var currentPoint = new Point((x.First().EventArgs.X + x.Last().EventArgs.X) / 2, (x.First().EventArgs.Y + x.Last().EventArgs.Y) / 2);
// Point currentPoint = new Point(emove.X, emove.Y);
graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
// graph.DrawLine(new Pen(Color.Blue, 2), startPoint, currentPoint);
Rectangle rect = new Rectangle(2 * currentPoint.X - startPoint.X, 2 * currentPoint.Y - startPoint.Y, 2 * (startPoint.X - currentPoint.X), 2 * (startPoint.Y - currentPoint.Y));
graph.DrawEllipse(new Pen(Color.Blue, 2), rect);
this.pictureBox1.Image = t;
}
);
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
//松开鼠标,取消订阅鼠标移动事件,并把绘制图像缓存一下,以便下次在次基础上绘制。
if (_mouseMoveSubscribe != null)
{
_tempImage = this.pictureBox1.Image;
_mouseMoveSubscribe.Dispose();
}
}
}
}