SimpleDraw-Windows Phone7上的应用
SimpleDraw是一款Windows Phone上的小應(yīng)用,下載地址是:http://www.windowsphone.com/en-US/apps/6011404f-fdec-4cb9-a982-ccac353dc146
下面把它的一些功能和開(kāi)發(fā)源代碼與大家分享一下。
該軟件實(shí)現(xiàn)直線,圓,柜形,任意線條的畫(huà)圖,同時(shí)能改變色彩和筆粗。
同時(shí)能對(duì)已有的圖片和攝像頭拍攝的圖片進(jìn)行增改。
代碼如下:(其他窗體代碼見(jiàn)源代碼文件)
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 class MainPanel : PhoneApplicationPage
??? {
??????? 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>();//撤銷(xiāo)棧
??????? /// <summary>
??????? /// 解決形狀重疊問(wèn)題
??????? /// </summary>
??????? /// <param name="shape"></param>
??????? /// <param name="X"></param>
??????? /// <param name="Y"></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);
??????????????????? }
??????????????? }
??????? }
??????? //畫(huà)圖過(guò)程
??????? 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);
??????????? }
??????? }
??????? //畫(huà)圖結(jié)束
??????? 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);
??????????? }
??????? }
??????? //畫(huà)圖開(kāi)始
??????? 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)//畫(huà)直線
??????????????? {
??????????????????? 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)//畫(huà)矩形
??????????????????? {
??????????????????????? 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)//畫(huà)橢圓
??????????????????????? {
??????????????????????????? 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//畫(huà)任意線條
??????????????????????????? {
??????????????????????????????? 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("/ColorPage.xaml", UriKind.RelativeOrAbsolute));
??????? }
??????? private void Width_Click(object sender, EventArgs e)
??????? {
??????????? NavigationService.Navigate(new Uri("/WidthPage.xaml", UriKind.RelativeOrAbsolute));
??????? }
??????? private void Shape_Click(object sender, EventArgs e)
??????? {
??????????? NavigationService.Navigate(new Uri("/ShapePage.xaml", 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 p_w_picpath = new Image();
??????????????? if (bitmap.PixelWidth > bitmap.PixelHeight)
??????????????? {
??????????????????? camermark = true;
??????????????????? p_w_picpath.Width = 696;
??????????????????? p_w_picpath.Height = 480;
??????????????????? p_w_picpath.Margin = new Thickness(-108, -110, 0, 0);
??????????????????? RotateTransform rt = new RotateTransform();
??????????????????? rt.Angle = 90;
??????????????????? rt.CenterX = 240;
??????????????????? rt.CenterY = 348;
??????????????????? p_w_picpath.RenderTransform = rt;
??????????????? }
??????????????? else
??????????????? {
??????????????????? camermark = false;
??????????????????? p_w_picpath.Width = 480;
??????????????????? p_w_picpath.Height = 696;
??????????????? }
??????????????? p_w_picpath.Stretch = Stretch.Uniform;
??????????????? p_w_picpath.Source = bitmap;
??????????????? canv.Children.Add(p_w_picpath);
??????????????? shapes.Push(p_w_picpath);
??????????? }
??????? }
??????? 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 p_w_picpath = new Image();
??????????????? p_w_picpath.Width = 696;
??????????????? p_w_picpath.Height = 480;
??????????????? p_w_picpath.Margin = new Thickness(-108, -110, 0, 0);
??????????????? p_w_picpath.Stretch = Stretch.Uniform;
??????????????? p_w_picpath.Source = bitmap;
??????????????? RotateTransform rt = new RotateTransform();
??????????????? rt.Angle = 90;
??????????????? rt.CenterX = 240;
??????????????? rt.CenterY = 348;
??????????????? p_w_picpath.RenderTransform = rt;
??????????????? canv.Children.Add(p_w_picpath);
??????????????? shapes.Push(p_w_picpath);
??????????? }
??????? }
??????? 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("/About.xaml", UriKind.RelativeOrAbsolute));
??????? }
??????? IsolatedStorageFile filestor = IsolatedStorageFile.GetUserStoreForApplication();
??????? /// <summary>
??????? /// 保存圖片
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></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("Draw.jpg", memorystream);
??????????????? memorystream.Close();
??????????????? camermark = false;
??????????????? canv.Children.Clear();
??????????????? MessageBox.Show("Sava successful!", "Message", MessageBoxButton.OK);?????????
??????????? }
??????????? catch (Exception exc)
??????????? {
??????????????? MessageBox.Show(exc.Message);
??????????? }
??????????? finally
??????????? {
??????????????? memorystream.Close();
??????????? }
??????? }
??? }
}
XAML代碼如下:
<phone:PhoneApplicationPage xmlns:my="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"?
??? x:Class="SaikoSimpleDraw.MainPanel"
??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??? xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
??? xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
??? xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
??? xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
??? FontFamily="{StaticResource PhoneFontFamilyNormal}"
??? FontSize="{StaticResource PhoneFontSizeNormal}"
??? Foreground="{StaticResource PhoneForegroundBrush}"
??? SupportedOrientations="Portrait" Orientation="Portrait"
??? mc:Ignorable="d" d:DesignHeight="696" d:DesignWidth="480"
??? shell:SystemTray.IsVisible="True" >
??? <!--LayoutRoot is the root grid where all page content is placed-->
??? <Grid x:Name="LayoutRoot" Background="Transparent">
??????? <!--ContentPanel - place additional content here-->
??????? <Canvas Width="480" Height="696" Name="canv"? Background="White"? ManipulationDelta="can_ManipulationDelta" ManipulationCompleted="can_ManipulationCompleted" ManipulationStarted="can_ManipulationStarted" >
??????? </Canvas>
??? </Grid>
??? <!--Sample code showing usage of ApplicationBar-->
??? <phone:PhoneApplicationPage.ApplicationBar>
??????? <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
??????????? <shell:ApplicationBarIconButton Click="GoBack_Click" IconUri="/Image/GoBack.png" Text="Cancel"/>
??????????? <shell:ApplicationBarIconButton Click="Color_Click" IconUri="/Image/Color.png" Text="Color"/>
??????????? <shell:ApplicationBarIconButton? Click="Width_Click" IconUri="/Image/Width.png" Text="Thickness"/>
??????????? <shell:ApplicationBarIconButton? Click="Shape_Click" IconUri="/Image/Shape.png" Text="Shape"/>
??????????? <shell:ApplicationBar.MenuItems>
??????????????? <shell:ApplicationBarMenuItem Text="Save" Click="Save_ApplicationBarMenuItem_Click"/>??
??????????????? <shell:ApplicationBarMenuItem Text="Clear" Click="Clear_ApplicationBarMenuItem_Click"/>
??????????????? <shell:ApplicationBarMenuItem Text="Switch Background" Click="Back_ApplicationBarMenuItem_Click"/>
??????????????? <shell:ApplicationBarMenuItem Text="Camera" Click="Pic_ApplicationBarMenuItem_Click"/>
??????????????? <shell:ApplicationBarMenuItem Text="Photo" Click="PicSel_ApplicationBarMenuItem_Click"/>
??????????????? <shell:ApplicationBarMenuItem Text="About" Click="About_ApplicationBarMenuItem_Click"/>
??????????? </shell:ApplicationBar.MenuItems>
??????? </shell:ApplicationBar>
??? </phone:PhoneApplicationPage.ApplicationBar>
</phone:PhoneApplicationPage>
?
轉(zhuǎn)載于:https://blog.51cto.com/axzxs/749442
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的SimpleDraw-Windows Phone7上的应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux 查看文件夹大小 du命令
- 下一篇: 命令行 sc delete 的使用(删除