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 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>();//撤销栈
/// <summary>
/// 解决形状重叠问题
/// </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);
}
}
}
//画图过程
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("/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 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("/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>
本文转自桂素伟51CTO博客,原文链接:http://blog.51cto.com/axzxs/749442 ,如需转载请自行联系原作者