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


相关文章
|
11月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
386 11
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
465 0
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
安全 前端开发 Windows
Windows Electron 应用更新的原理是什么?揭秘 NsisUpdater
本文介绍了 Electron 应用在 Windows 中的更新原理,重点分析了 `NsisUpdater` 类的实现。该类利用 NSIS 脚本,通过初始化、检查更新、下载更新、验证签名和安装更新等步骤,确保应用的更新过程安全可靠。核心功能包括差异下载、签名验证和管理员权限处理,确保更新高效且安全。
744 4
Windows Electron 应用更新的原理是什么?揭秘 NsisUpdater
|
Unix Linux Ruby
在windows和linux上高效快捷地发布Dash应用
在windows和linux上高效快捷地发布Dash应用
921 4
|
Linux iOS开发 开发者
跨平台开发不再难:.NET Core如何让你的应用在Windows、Linux、macOS上自如游走?
【8月更文挑战第28天】本文提供了一份详尽的.NET跨平台开发指南,涵盖.NET Core简介、环境配置、项目结构、代码编写、依赖管理、构建与测试、部署及容器化等多个方面,帮助开发者掌握关键技术与最佳实践,充分利用.NET Core实现高效、便捷的跨平台应用开发与部署。
1959 3
|
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功能,从而提升用户体验并拓展应用功能边界。
597 1
|
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. 错误
431 1
|
XML 缓存 前端开发
Electron-builder 是如何打包 Windows 应用的?
本文首发于微信公众号“前端徐徐”,作者徐徐深入解析了 electron-builder 在 Windows 平台上的打包流程。文章详细介绍了 `winPackager.ts`、`AppxTarget.ts`、`MsiTarget.ts` 和 `NsisTarget.ts` 等核心文件,涵盖了目标创建、图标处理、代码签名、资源编辑、应用签名、性能优化等内容,并分别讲解了 AppX/MSIX、MSI 和 NSIS 安装程序的生成过程。通过这些内容,读者可以更好地理解和使用 electron-builder 进行 Windows 应用的打包和发布。
1483 0
|
Windows
【Windows】 Win10下报错:该文件没有与之关联的应用来执行该操作。请安装应用,若已经安装应用,请在“默认应用设置”页面中创建关联
【Windows】 Win10下报错:该文件没有与之关联的应用来执行该操作。请安装应用,若已经安装应用,请在“默认应用设置”页面中创建关联
2467 1
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
469 0