博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【WP7】绘图与保存
阅读量:6948 次
发布时间:2019-06-27

本文共 2672 字,大约阅读时间需要 8 分钟。

WP7提供了InkPresenter控件用于绘图,使用InkPresenter控件绘图很简单

1、首先新建一个InkPresenter控件,然后调用CaptureMouse()方法对鼠标进行捕获(这样才能知道绘图时鼠标具体的坐标)

  inkPresenter1.CaptureMouse();

2、添加一个全局变量,用于收集鼠标的坐标集合

  Stroke newStroke;

3、添加三个事件  MouseLeftButtonDown  MouseMove   LostMouseCapture

private void inkPresenter1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            inkPresenter1.CaptureMouse();//开始捕获鼠标移动路径            newStroke = new Stroke();            DrawingAttributes draw = new DrawingAttributes();            draw.Color = Colors.Blue;            draw.Width = 10;            draw.Height = 10;            draw.OutlineColor = Colors.Red;            newStroke.DrawingAttributes = draw;            newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter1));            this.inkPresenter1.Strokes.Add(newStroke);        }        private void inkPresenter1_MouseMove(object sender, MouseEventArgs e)        {            Point p = e.GetPosition(inkPresenter1);            if (p.X < inkPresenter1.Width && p.Y < inkPresenter1.Height && p.X > 0 && p.Y > 0)            {                if (newStroke != null)                {                    //记录鼠标在inkPresenter1上的移动的点                    newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter1));                }            }            else            {                this.newStroke = null;            }        }        private void inkPresenter1_LostMouseCapture(object sender, MouseEventArgs e)        {            this.newStroke = null;        }

接下来是如何保存控件,这个直接上代码,保存图片到隔离存储控件的 MyPicture/pic1.jpg

  WriteableBitmap 也可以保存其他控件的外观

WriteableBitmap bitmap = new WriteableBitmap(inkPresenter1, null);        using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())        {            string directory = "MyPicture/";            string jpg = directory + "/pic1.jpg";            if (!file.DirectoryExists(directory))            {                file.CreateDirectory(directory);            }            using (IsolatedStorageFileStream stream = file.OpenFile(jpg, System.IO.FileMode.OpenOrCreate))            {                bitmap.SaveJpeg(stream, 173, 173, 0, 100);            }        }

接下来是访问刚刚保存的图片文件,添加一个Image控件image1用于显示刚刚保存的图片

using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())        {            string filename = "MyPicture/pic1.jpg";            using (IsolatedStorageFileStream stream = file.OpenFile(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))            {                BitmapImage bmp = new BitmapImage();                bmp.SetSource(stream);                this.image1.Source = bmp;            }        }

 

 

转载地址:http://zphnl.baihongyu.com/

你可能感兴趣的文章
ArcGIS Engine 中的多线程使用 (转载)
查看>>
linux下c的网络编程---转载
查看>>
filter中的DelegatingFilterProxy使用事例
查看>>
WinForm 天猫2013双11自动抢红包【源码下载】
查看>>
学习数学从《数学之美》开始
查看>>
flashcache的实现与分析
查看>>
[UML]UML系列——状态机图statechart diagram
查看>>
微信公众平台开发(74) 用户分组管理
查看>>
二、jdk命令之javah命令(C Header and Stub File Generator)
查看>>
ios模拟器未能安装此应用程序
查看>>
站长常用的200个js代码 站长常用js代码大全 站长常用js代码集合
查看>>
HBase eclipse开发环境搭建
查看>>
SQL Server - 把星期一(周一)当作每个星期的开始在一年中求取周数
查看>>
【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
查看>>
jquery-alert对话框
查看>>
WIN8系统安装软件时提示"扩展属性不一致"的解决方法
查看>>
sqlite3.exe 使用
查看>>
微软职位内部推荐-Senior Software Engineer
查看>>
CAD中批量打印
查看>>
蛋疼的Apple IOS Push通知协议
查看>>