设为首页 收藏本站
查看: 724|回复: 0

[经验分享] SimpleDraw-Windows Phone7上的应用

[复制链接]

尚未签到

发表于 2018-6-27 10:14:28 | 显示全部楼层 |阅读模式
  SimpleDraw是一款Windows Phone上的小应用,下载地址是:http://www.windowsphone.com/en-US/apps/6011404f-fdec-4cb9-a982-ccac353dc146
  下面把它的一些功能和开发源代码与大家分享一下。
  该软件实现直线,圆,柜形,任意线条的画图,同时能改变色彩和笔粗。
  同时能对已有的图片和摄像头拍摄的图片进行增改。
  代码如下:(其他窗体代码见源代码文件)
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Animation;
  using System.Windows.Shapes;
  using Microsoft.Phone.Controls;
  using Microsoft.Phone.Info;
  using Microsoft.Phone.Tasks;
  using System.Windows.Media.Imaging;
  using System.IO.IsolatedStorage;
  using System.IO;
  using Microsoft.Xna.Framework.Media;
  namespace SaikoSimpleDraw
  {

  public partial>  {
  public MainPanel()
  {
  InitializeComponent();
  try
  {
  this.transofrmGroup = new TransformGroup();
  translation = new TranslateTransform();
  scale = new ScaleTransform();
  this.transofrmGroup.Children.Add(translation);
  this.transofrmGroup.Children.Add(scale);
  canv.RenderTransform = this.transofrmGroup;
  cct = new CameraCaptureTask();
  cct.Completed += CamerCaptureTask_Completed;
  pct = new PhotoChooserTask();
  pct.Completed += PhotoCaptureTask_Completed;
  }
  catch (Exception exc)
  {
  MessageBox.Show(exc.Message);
  }
  }
  CameraCaptureTask cct;
  PhotoChooserTask pct;
  TransformGroup transofrmGroup;
  TranslateTransform translation;
  ScaleTransform scale;
  Polyline polyline;
  Line line;
  Rectangle rectangle;
  Ellipse ellipse;
  Polygon polygon;
  Point RecPoint = new Point();
  Stack<UIElement> shapes = new Stack<UIElement>();//撤销栈
  /// <summary>
  /// 解决形状重叠问题
  /// </summary>
  /// <param name=&quot;shape&quot;></param>
  /// <param name=&quot;X&quot;></param>
  /// <param name=&quot;Y&quot;></param>
  void GetXY(UIElement shape, ref double X, ref double Y)
  {
  if (shape is Rectangle)
  {
  X += ((Rectangle)shape).Margin.Left;
  Y += ((Rectangle)shape).Margin.Top;
  }
  else
  if (shape is Ellipse)
  {
  X += ((Ellipse)shape).Margin.Left;
  Y += ((Ellipse)shape).Margin.Top;
  }
  else
  {
  if (shape is Image)
  {
  X += 240 - (348 - ((Image)shape).Margin.Top);
  Y += 348 - (240 - ((Image)shape).Margin.Left);
  }
  }
  }
  //画图过程
  private void can_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
  {
  try
  {
  double X = e.ManipulationOrigin.X;
  double Y = e.ManipulationOrigin.Y;
  if (camermark || App.FillOff)
  {
  if (e.ManipulationContainer is Image)
  {
  X = 696 - e.ManipulationOrigin.Y;
  Y = e.ManipulationOrigin.X;
  }
  GetXY(e.ManipulationContainer, ref X, ref Y);
  }
  if (App.shape is Line)
  {
  line.X2 = X;
  line.Y2 = Y;
  }
  else
  if (App.shape is Rectangle)
  {
  double x = 0, y = 0;
  x = RecPoint.X > X ? X : RecPoint.X;
  y = RecPoint.Y > Y ? Y : RecPoint.Y;
  rectangle.Margin = new Thickness(x, y, 0, 0);
  rectangle.Width = Math.Abs(X - RecPoint.X);
  rectangle.Height = Math.Abs(Y - RecPoint.Y);
  }
  else
  {
  if (App.shape is Ellipse)
  {
  double x = 0, y = 0;
  x = RecPoint.X > X ? X : RecPoint.X;
  y = RecPoint.Y > Y ? Y : RecPoint.Y;
  ellipse.Margin = new Thickness(x, y, 0, 0);
  ellipse.Width = Math.Abs(X - RecPoint.X);
  ellipse.Height = Math.Abs(Y - RecPoint.Y);
  }
  else
  if (App.shape is Polygon)
  {
  polygon.Points.Add(new Point(X, Y));
  }
  else
  {
  polyline.Points.Add(new Point(X, Y));
  }
  }
  }
  catch (Exception exc)
  {
  MessageBox.Show(exc.Message);
  }
  }
  //画图结束
  private void can_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
  {
  try
  {
  double X = e.ManipulationOrigin.X;
  double Y = e.ManipulationOrigin.Y;
  if (camermark || App.FillOff)
  {
  if (e.ManipulationContainer is Image)
  {
  X = 696 - e.ManipulationOrigin.Y;
  Y = e.ManipulationOrigin.X;
  }
  GetXY(e.ManipulationContainer, ref X, ref Y);
  }
  if (App.shape is Line)
  {
  line.X2 = X;
  line.Y2 = Y;
  }
  else
  if (App.shape is Rectangle)
  {
  double x = 0, y = 0;
  x = RecPoint.X > X ? X : RecPoint.X;
  y = RecPoint.Y > Y ? Y : RecPoint.Y;
  rectangle.Margin = new Thickness(x, y, 0, 0);
  rectangle.Width = Math.Abs(X - RecPoint.X);
  rectangle.Height = Math.Abs(Y - RecPoint.Y);
  }
  else
  {
  if (App.shape is Ellipse)
  {
  double x = 0, y = 0;
  x = RecPoint.X > X ? X : RecPoint.X;
  y = RecPoint.Y > Y ? Y : RecPoint.Y;
  ellipse.Margin = new Thickness(x, y, 0, 0);
  ellipse.Width = Math.Abs(X - RecPoint.X);
  ellipse.Height = Math.Abs(Y - RecPoint.Y);
  }
  else
  if (App.shape is Polygon)
  {
  polygon.Points.Add(new Point(X, Y));
  }
  else
  {
  polyline.Points.Add(new Point(X, Y));
  }
  }
  }
  catch (Exception exc)
  {
  MessageBox.Show(exc.Message);
  }
  }
  //画图开始
  private void can_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
  {
  try
  {
  double X = e.ManipulationOrigin.X;
  double Y = e.ManipulationOrigin.Y;
  if (camermark || App.FillOff)
  {
  if (e.ManipulationContainer is Image)
  {
  X = 696 - e.ManipulationOrigin.Y;
  Y = e.ManipulationOrigin.X;
  }
  GetXY(e.ManipulationContainer, ref X, ref Y);
  }
  if (App.shape is Line)//画直线
  {
  line = new Line();
  line.StrokeThickness = App.width;
  line.Stroke = App.brush;
  line.X1 = X;
  line.Y1 = Y;
  line.X2 = X;
  line.Y2 = Y;
  canv.Children.Add(line);
  shapes.Push(line);
  }
  else
  if (App.shape is Rectangle)//画矩形
  {
  rectangle = new Rectangle();
  rectangle.Stroke = App.brush;
  rectangle.StrokeThickness = App.width;
  RecPoint.X = X;
  RecPoint.Y = Y;
  if (App.FillOff)
  {
  rectangle.Fill = App.brush;
  }
  rectangle.Margin = new Thickness(RecPoint.X, RecPoint.Y, 0, 0);
  rectangle.Height = RecPoint.Y;
  canv.Children.Add(rectangle);
  shapes.Push(rectangle);
  }
  else
  {
  if (App.shape is Ellipse)//画椭圆
  {
  ellipse = new Ellipse();
  ellipse.Stroke = App.brush;
  ellipse.StrokeThickness = App.width;
  RecPoint.X = X;
  RecPoint.Y = Y;
  ellipse.Margin = new Thickness(RecPoint.X, RecPoint.Y, 0, 0);
  ellipse.Height = RecPoint.Y;
  if (App.FillOff)
  {
  ellipse.Fill = App.brush;
  }
  canv.Children.Add(ellipse);
  shapes.Push(ellipse);
  }
  else
  if (App.shape is Polygon)//任意多边形
  {
  polygon = new Polygon();
  polygon.StrokeThickness = App.width;
  polygon.Stroke = App.brush;
  if (App.FillOff)
  {
  polygon.Fill = App.brush;
  }
  canv.Children.Add(polygon);
  shapes.Push(polygon);
  polygon.Points.Add(new Point(X, Y));
  }
  else//画任意线条
  {
  polyline = new Polyline();
  polyline.StrokeThickness = App.width;
  polyline.Stroke = App.brush;
  canv.Children.Add(polyline);
  shapes.Push(polyline);
  polyline.Points.Add(new Point(X, Y));
  }
  }
  }
  catch (Exception exc)
  {
  MessageBox.Show(exc.Message);
  }
  }
  private void GoBack_Click(object sender, EventArgs e)
  {
  if (shapes.Count > 0)
  {
  canv.Children.Remove(shapes.Pop());
  }
  }
  private void Color_Click(object sender, EventArgs e)
  {
  NavigationService.Navigate(new Uri(&quot;/ColorPage.xaml&quot;, UriKind.RelativeOrAbsolute));
  }

  private void>  {
  NavigationService.Navigate(new Uri(&quot;/WidthPage.xaml&quot;, UriKind.RelativeOrAbsolute));
  }
  private void Shape_Click(object sender, EventArgs e)
  {
  NavigationService.Navigate(new Uri(&quot;/ShapePage.xaml&quot;, UriKind.RelativeOrAbsolute));
  }
  void PicSel_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  pct.Show();
  }
  void PhotoCaptureTask_Completed(object sender, PhotoResult e)
  {
  camermark = false;
  if (e.TaskResult == TaskResult.OK)
  {
  BitmapImage bitmap = new BitmapImage();
  bitmap.SetSource(e.ChosenPhoto);
  Image image = new Image();
  if (bitmap.PixelWidth > bitmap.PixelHeight)
  {
  camermark = true;
  image.Width = 696;
  image.Height = 480;
  image.Margin = new Thickness(-108, -110, 0, 0);
  RotateTransform rt = new RotateTransform();
  rt.Angle = 90;
  rt.CenterX = 240;
  rt.CenterY = 348;
  image.RenderTransform = rt;
  }
  else
  {
  camermark = false;
  image.Width = 480;
  image.Height = 696;
  }
  image.Stretch = Stretch.Uniform;
  image.Source = bitmap;
  canv.Children.Add(image);
  shapes.Push(image);
  }
  }
  private void Pic_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  cct.Show();
  }
  bool camermark = false;
  public void CamerCaptureTask_Completed(Object sender, PhotoResult e)
  {
  camermark = true;
  if (e.TaskResult == TaskResult.OK)
  {
  BitmapImage bitmap = new BitmapImage();
  bitmap.SetSource(e.ChosenPhoto);
  Image image = new Image();
  image.Width = 696;
  image.Height = 480;
  image.Margin = new Thickness(-108, -110, 0, 0);
  image.Stretch = Stretch.Uniform;
  image.Source = bitmap;
  RotateTransform rt = new RotateTransform();
  rt.Angle = 90;
  rt.CenterX = 240;
  rt.CenterY = 348;
  image.RenderTransform = rt;
  canv.Children.Add(image);
  shapes.Push(image);
  }
  }
  private void Clear_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  canv.Children.Clear();
  }
  bool backcolor = true;
  private void Back_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  if (backcolor)
  {
  canv.Background = new SolidColorBrush(Colors.Black);
  }
  else
  {
  canv.Background = new SolidColorBrush(Colors.White);
  }
  backcolor = !backcolor;
  }
  private void About_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  NavigationService.Navigate(new Uri(&quot;/About.xaml&quot;, UriKind.RelativeOrAbsolute));
  }
  IsolatedStorageFile filestor = IsolatedStorageFile.GetUserStoreForApplication();
  /// <summary>
  /// 保存图片
  /// </summary>
  /// <param name=&quot;sender&quot;></param>
  /// <param name=&quot;e&quot;></param>
  private void Save_ApplicationBarMenuItem_Click(object sender, EventArgs e)
  {
  MemoryStream memorystream = new MemoryStream();
  try
  {
  ScaleTransform trans = new ScaleTransform();
  trans.ScaleX = 1;
  trans.ScaleY = 0.5;
  WriteableBitmap wbitmap = new WriteableBitmap(canv, trans);
  wbitmap.SaveJpeg(memorystream, Convert.ToInt32(canv.Width), Convert.ToInt32(canv.Height), 0, 90);
  wbitmap.Invalidate();
  memorystream.Seek(0, SeekOrigin.Begin);
  MediaLibrary lib = new MediaLibrary();
  lib.SavePicture(&quot;Draw.jpg&quot;, memorystream);
  memorystream.Close();
  camermark = false;
  canv.Children.Clear();
  MessageBox.Show(&quot;Sava successful!&quot;, &quot;Message&quot;, MessageBoxButton.OK);
  }
  catch (Exception exc)
  {
  MessageBox.Show(exc.Message);
  }
  finally
  {
  memorystream.Close();
  }
  }
  }
  }
  XAML代码如下:
  <phone:PhoneApplicationPage xmlns:my=&quot;clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI&quot;
  x:Class=&quot;SaikoSimpleDraw.MainPanel&quot;
  xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  xmlns:phone=&quot;clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone&quot;
  xmlns:shell=&quot;clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone&quot;
  xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  FontFamily=&quot;{StaticResource PhoneFontFamilyNormal}&quot;
  FontSize=&quot;{StaticResource PhoneFontSizeNormal}&quot;
  Foreground=&quot;{StaticResource PhoneForegroundBrush}&quot;
  SupportedOrientations=&quot;Portrait&quot; Orientation=&quot;Portrait&quot;
  mc:Ignorable=&quot;d&quot; d:DesignHeight=&quot;696&quot; d:DesignWidth=&quot;480&quot;
  shell:SystemTray.IsVisible=&quot;True&quot; >
  <!--LayoutRoot is the root grid where all page content is placed-->
  <Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;Transparent&quot;>
  <!--ContentPanel - place additional content here-->

  <Canvas>  </Canvas>
  </Grid>
  <!--Sample code showing usage of ApplicationBar-->
  <phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible=&quot;True&quot; IsMenuEnabled=&quot;True&quot;>
  <shell:ApplicationBarIconButton Click=&quot;GoBack_Click&quot; IconUri=&quot;/Image/GoBack.png&quot; Text=&quot;Cancel&quot;/>
  <shell:ApplicationBarIconButton Click=&quot;Color_Click&quot; IconUri=&quot;/Image/Color.png&quot; Text=&quot;Color&quot;/>
  <shell:ApplicationBarIconButton  Click=&quot;Width_Click&quot; IconUri=&quot;/Image/Width.png&quot; Text=&quot;Thickness&quot;/>
  <shell:ApplicationBarIconButton  Click=&quot;Shape_Click&quot; IconUri=&quot;/Image/Shape.png&quot; Text=&quot;Shape&quot;/>
  <shell:ApplicationBar.MenuItems>
  <shell:ApplicationBarMenuItem Text=&quot;Save&quot; Click=&quot;Save_ApplicationBarMenuItem_Click&quot;/>
  <shell:ApplicationBarMenuItem Text=&quot;Clear&quot; Click=&quot;Clear_ApplicationBarMenuItem_Click&quot;/>
  <shell:ApplicationBarMenuItem Text=&quot;Switch Background&quot; Click=&quot;Back_ApplicationBarMenuItem_Click&quot;/>
  <shell:ApplicationBarMenuItem Text=&quot;Camera&quot; Click=&quot;Pic_ApplicationBarMenuItem_Click&quot;/>
  <shell:ApplicationBarMenuItem Text=&quot;Photo&quot; Click=&quot;PicSel_ApplicationBarMenuItem_Click&quot;/>
  <shell:ApplicationBarMenuItem Text=&quot;About&quot; Click=&quot;About_ApplicationBarMenuItem_Click&quot;/>
  </shell:ApplicationBar.MenuItems>
  </shell:ApplicationBar>
  </phone:PhoneApplicationPage.ApplicationBar>
  </phone:PhoneApplicationPage>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-531255-1-1.html 上篇帖子: Windows 7 几个小问题的解决方法 下篇帖子: windows phone (26) ApplicationBar应用程序栏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表