SimpleDraw-Windows Phone7上的应用

简介:

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 ,如需转载请自行联系原作者


相关文章
|
13天前
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
10天前
|
Unix Linux Ruby
在windows和linux上高效快捷地发布Dash应用
在windows和linux上高效快捷地发布Dash应用
|
10天前
|
Linux iOS开发 开发者
跨平台开发不再难:.NET Core如何让你的应用在Windows、Linux、macOS上自如游走?
【8月更文挑战第28天】本文提供了一份详尽的.NET跨平台开发指南,涵盖.NET Core简介、环境配置、项目结构、代码编写、依赖管理、构建与测试、部署及容器化等多个方面,帮助开发者掌握关键技术与最佳实践,充分利用.NET Core实现高效、便捷的跨平台应用开发与部署。
45 3
|
13天前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
|
7天前
|
vr&ar C# 图形学
WPF与AR/VR的激情碰撞:解锁Windows Presentation Foundation应用新维度,探索增强现实与虚拟现实技术在现代UI设计中的无限可能与实战应用详解
【8月更文挑战第31天】增强现实(AR)与虚拟现实(VR)技术正迅速改变生活和工作方式,在游戏、教育及工业等领域展现出广泛应用前景。本文探讨如何在Windows Presentation Foundation(WPF)环境中实现AR/VR功能,通过具体示例代码展示整合过程。尽管WPF本身不直接支持AR/VR,但借助第三方库如Unity、Vuforia或OpenVR,可实现沉浸式体验。例如,通过Unity和Vuforia在WPF中创建AR应用,或利用OpenVR在WPF中集成VR功能,从而提升用户体验并拓展应用功能边界。
18 0
|
7天前
|
存储 开发者 C#
WPF与邮件发送:教你如何在Windows Presentation Foundation应用中无缝集成电子邮件功能——从界面设计到代码实现,全面解析邮件发送的每一个细节密武器!
【8月更文挑战第31天】本文探讨了如何在Windows Presentation Foundation(WPF)应用中集成电子邮件发送功能,详细介绍了从创建WPF项目到设计用户界面的全过程,并通过具体示例代码展示了如何使用`System.Net.Mail`命名空间中的`SmtpClient`和`MailMessage`类来实现邮件发送逻辑。文章还强调了安全性和错误处理的重要性,提供了实用的异常捕获代码片段,旨在帮助WPF开发者更好地掌握邮件发送技术,提升应用程序的功能性与用户体验。
15 0
|
7天前
|
C# Windows 监控
WPF应用跨界成长秘籍:深度揭秘如何与Windows服务完美交互,扩展功能无界限!
【8月更文挑战第31天】WPF(Windows Presentation Foundation)是 .NET 框架下的图形界面技术,具有丰富的界面设计和灵活的客户端功能。在某些场景下,WPF 应用需与 Windows 服务交互以实现后台任务处理、系统监控等功能。本文探讨了两者交互的方法,并通过示例代码展示了如何扩展 WPF 应用的功能。首先介绍了 Windows 服务的基础知识,然后阐述了创建 Windows 服务、设计通信接口及 WPF 客户端调用服务的具体步骤。通过合理的交互设计,WPF 应用可获得更强的后台处理能力和系统级操作权限,提升应用的整体性能。
21 0
|
13天前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
14天前
|
Python Windows 内存技术
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
|
26天前
|
Windows
Windows——如何提取Microsoft Store的应用
Windows——如何提取Microsoft Store的应用
23 0
下一篇
DDNS