silverlight:手写板/涂鸦/墨迹/InkPresenter示例程序
这种应用现在已经比较常见了,比如论坛回贴中的手写功能 ,IM聊天中的个性化手写文字,个性签名等,在Silverlight中要实现该功能其实非常简单,只要一个InkPresenter控件即可
使用要点:
1.要合理设置裁剪区,否则手写时可能笔划会写到你不希望出现的地方.
2.处理好MouseLeftButtonDown,MouseMove,LostMouseCapture这三个事件.
演示代码:
前端Xaml部分:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="Black"
>
<Grid Background="#FF0BB5A3" Width="650" Height="615">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock x:Name="txtTitle" Text="InkPresenter 演 示" FontWeight="Bold" Grid.Row="0" HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Foreground="White" />
<StackPanel x:Name="spToolBar" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Height="25" VerticalAlignment="Center">
<TextBlock Text="笔划颜色:" VerticalAlignment="Center"></TextBlock>
<ComboBox x:Name="cboColor" Width="65" VerticalAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Color}" ToolTipService.ToolTip="{Binding Name}" Width="10" Height="10"/>
<TextBlock Text="{Binding Name}" Margin="2,0,0,0" Foreground="{Binding Color}"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="笔划外框颜色:" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>
<ComboBox x:Name="cboOutlineColor" Width="65" VerticalAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Color}" ToolTipService.ToolTip="{Binding Name}" Width="10" Height="10"/>
<TextBlock Text="{Binding Name}" Margin="2,0,0,0" Foreground="{Binding Color}"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="笔划宽度:" Margin="10,0,0,0" VerticalAlignment="Center"></TextBlock>
<ComboBox x:Name="cboWidth" Width="60" VerticalAlignment="Center" Height="20">
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Black" Width="40" Height="{Binding Size}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="笔划高度:" Margin="10,0,0,0" VerticalAlignment="Center"></TextBlock>
<ComboBox x:Name="cboHeight" Width="60" VerticalAlignment="Center" Height="20">
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Black" Width="40" Height="{Binding Size}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Text="透明度:" Margin="10,0,0,0" VerticalAlignment="Center"></TextBlock>
<ComboBox x:Name="cboOpactiy" Width="60" VerticalAlignment="Center" SelectionChanged="cboOpactiy_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value}" Opacity="{Binding Value}" ></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
<Canvas x:Name="inkC" Grid.Row="2" Height="370">
<Rectangle x:Name="rectBg" Grid.Row="2" Width="650" Height="370" Fill="#FFECD85C" Margin="0" StrokeThickness="0"></Rectangle>
<Image Source="image/background.png" Stretch="UniformToFill" x:Name="imgBg"></Image>
<InkPresenter x:Name="ink" Height="370" Width="650" Grid.Row="2"
Background="Transparent" Opacity="1"
MouseLeftButtonDown="OnMouseLeftButtonDown"
MouseMove="OnMouseMove"
LostMouseCapture="OnLostMouseCapture" Margin="0"
>
<InkPresenter.Clip>
<RectangleGeometry Rect="0,0,650,370"></RectangleGeometry>
</InkPresenter.Clip>
</InkPresenter>
<TextBlock Text="by 菩提树下的杨过" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" Cursor="Hand" Canvas.Left="5" Canvas.Top="5" Foreground="LightGray"></TextBlock>
</Canvas>
<StackPanel x:Name="spBtn" Grid.Row="3" Height="30" Margin="0" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Name="btnToogleBg" HorizontalAlignment="Center" Width="80" Content="隐藏背景图" VerticalAlignment="Center" Click="btnToogleBg_Click"/>
<Button x:Name="btnToogleBgRect" HorizontalAlignment="Center" Width="80" Content="隐藏背景色" VerticalAlignment="Center" Click="btnToogleBgRect_Click" Margin="10,0,0,0"/>
<Button x:Name="btnSave" HorizontalAlignment="Center" Width="80" Content="保存图片" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnSave_Click"/>
<Button x:Name="btnSaveLocal" HorizontalAlignment="Center" Width="90" Content="保存图片到本地" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnSaveLocal_Click" />
<Button x:Name="btnClear" HorizontalAlignment="Center" Width="80" Content="清空画版" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnClear_Click"/>
<Button x:Name="btnClearSave" HorizontalAlignment="Center" Width="80" Content="清空保存区" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnClearSave_Click"/>
</StackPanel>
<ScrollViewer x:Name="ScreenshotViewer" Margin="0" Grid.Row="4" Background="#FFFFFF99" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden">
<StackPanel x:Name="thumbs" Orientation="Horizontal"/>
</ScrollViewer>
</Grid>
</UserControl>
后端代码:
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.IO;
using FluxJpeg.Core;
namespace InkPresenterTest
{
public partial class Page : UserControl
{
Stroke _drawStroke;
List<FillColor> lstFillColor;
List<SizeData> lstSizeData;
List<OpacityData> lstOpacityData;
bool _isLoaded = false;
public Page()
{
InitializeComponent();
//初始化数据
lstFillColor = new List<FillColor>() {
new FillColor(){ Color = new SolidColorBrush(Colors.Black), Name="黑色"},
new FillColor(){ Color = new SolidColorBrush(Colors.Red), Name="红色"},
new FillColor(){ Color = new SolidColorBrush(Colors.Blue), Name="蓝色"},
new FillColor(){ Color = new SolidColorBrush(Colors.Green),Name="绿色"},
new FillColor(){ Color = new SolidColorBrush(Colors.Magenta), Name="洋红"},
new FillColor(){ Color = new SolidColorBrush(Colors.Orange), Name="橙色"},
};
lstSizeData = new List<SizeData>()
{
new SizeData(){ Size=1.0},
new SizeData(){ Size=3.0},
new SizeData(){ Size=5.0},
new SizeData(){ Size=7.0},
new SizeData(){ Size=9.0},
new SizeData(){ Size=11.0},
new SizeData(){ Size=13.0},
new SizeData(){ Size=15.0}
};
lstOpacityData = new List<OpacityData>(){
new OpacityData(){ Value=0.1},
new OpacityData(){ Value=0.2},
new OpacityData(){ Value=0.3},
new OpacityData(){ Value=0.4},
new OpacityData(){ Value=0.5},
new OpacityData(){ Value=0.6},
new OpacityData(){ Value=0.7},
new OpacityData(){ Value=0.8},
new OpacityData(){ Value=0.9},
new OpacityData(){ Value=1.0}
};
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.cboColor.ItemsSource = lstFillColor;
this.cboColor.SelectedIndex = 0;
this.cboOutlineColor.ItemsSource = lstFillColor;
this.cboOutlineColor.SelectedIndex = 0;
this.cboWidth.ItemsSource = lstSizeData;
this.cboWidth.SelectedIndex = 0;
this.cboHeight.ItemsSource = lstSizeData;
this.cboHeight.SelectedIndex = 0;
this.cboOpactiy.ItemsSource = lstOpacityData;
this.cboOpactiy.SelectedIndex = 5;
_isLoaded = true;
}
private void OnMouseLeftButtonDown(object sender, MouseEventArgs e)
{
ink.CaptureMouse();
StylusPointCollection MyStylusPointCollection = new StylusPointCollection();
MyStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(ink));
_drawStroke = new Stroke(MyStylusPointCollection);
_drawStroke.DrawingAttributes.Color = (cboColor.SelectedItem as FillColor).Color.Color;
_drawStroke.DrawingAttributes.OutlineColor = (this.cboOutlineColor.SelectedItem as FillColor).Color.Color;
_drawStroke.DrawingAttributes.Width = (this.cboWidth.SelectedItem as SizeData).Size;
_drawStroke.DrawingAttributes.Height = (this.cboHeight.SelectedItem as SizeData).Size;
//_drawStroke.SetValue(OpacityProperty, (this.cboOpactiy.SelectedItem as OpacityData).Value);
ink.Strokes.Add(_drawStroke);
ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_drawStroke != null)
{
_drawStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(ink));
}
}
private void OnLostMouseCapture(object sender, MouseEventArgs e)
{
_drawStroke = null;
}
private void btnClear_Click(object sender, System.Windows.RoutedEventArgs e)
{
ink.Strokes.Clear();
}
private void cboOpactiy_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (_isLoaded)
{
ink.Opacity = (cboOpactiy.SelectedItem as OpacityData).Value;
}
}
private void btnToogleBg_Click(object sender, RoutedEventArgs e)
{
Button btn = (sender as Button);
if (btn.Content.ToString() == "隐藏背景图")
{
imgBg.Visibility = Visibility.Collapsed;
btn.Content = "显示背景图";
}
else
{
imgBg.Visibility = Visibility.Visible;
btn.Content = "隐藏背景图";
}
}
private void btnToogleBgRect_Click(object sender, RoutedEventArgs e)
{
Button btn = (sender as Button);
if (btn.Content.ToString() == "隐藏背景色")
{
this.rectBg.Visibility = Visibility.Collapsed;
btn.Content = "显示背景色";
}
else
{
rectBg.Visibility = Visibility.Visible;
btn.Content = "隐藏背景色";
}
}
private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
HtmlPage.Window.Navigate(new System.Uri("http://yjmyzz.cnblogs.com/"), "_blank");
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// 创建一个WriteableBitmap并且把需要呈现位图的元素赋值给WriteableBitmap
WriteableBitmap wb = new WriteableBitmap(inkC, null);
// 创建一个Image元素来承载位图
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
image.Height = 120;
image.Margin = new Thickness(5);
image.Source = wb;
// 将Image元素放入容器控件中
thumbs.Children.Add(image);
ScreenshotViewer.ScrollToHorizontalOffset(ScreenshotViewer.ExtentWidth);
}
private void btnClearSave_Click(object sender, RoutedEventArgs e)
{
thumbs.Children.Clear();
}
private void btnSaveLocal_Click(object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap(inkC, null);
if (wb != null)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "JPEG Files (*.jpeg)|*.jpeg";
saveDlg.DefaultExt = ".jpeg";
if (saveDlg.ShowDialog().Value)
{
using (Stream fs = saveDlg.OpenFile())
{
SaveToFile(wb, fs);
MessageBox.Show(string.Format("文件已经保存至“{0}”",saveDlg.SafeFileName));
}
}
}
}
private void SaveToFile(WriteableBitmap bitmap, Stream fs)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3;
byte[][,] raster = new byte[bands][,];
for (int i = 0; i < bands; i++)
{
raster[i] = new byte[width, height];
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
int pixel = bitmap.Pixels[width * row + column];
raster[0][column, row] = (byte)(pixel >> 16);
raster[1][column, row] = (byte)(pixel >> 8);
raster[2][column, row] = (byte)pixel;
}
}
FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel { colorspace = FluxJpeg.Core.ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
//Encode the Image as a JPEG
MemoryStream stream = new MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
encoder.Encode();
//Back to the start
stream.Seek(0, SeekOrigin.Begin);
//Get teh Bytes and write them to the stream
byte[] binaryData = new byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
fs.Write(binaryData, 0, binaryData.Length);
}
}
public class FillColor
{
public SolidColorBrush Color { set; get; }
public string Name { set; get; }
}
public class SizeData
{
public double Size { set; get; }
}
public class OpacityData
{
public double Value { set; get; }
}
}
源代码下载地址: http://files.cnblogs.com/yjmyzz/InkPresenterTest.rar
对Flash感兴趣的朋友,做为对比,也可以看下Flash/Flex学习笔记(14):制作涂鸦板
转载请注明来自菩提树下的杨过 http://www.cnblogs.com/yjmyzz/archive/2010/01/14/1647636.html
注:里面用到了一个开源的组件FJCore
silverlight:手写板/涂鸦/墨迹/InkPresenter示例程序相关推荐
- MindSpore部署图像分割示例程序
MindSpore部署图像分割示例程序 本端侧图像分割Android示例程序使用Java实现,Java层主要通过Android Camera 2 API实现摄像头获取图像帧,进行相应的图像处理,之后调 ...
- BizTalk 2006 简单入门示例程序(附源项目文件下载)
BizTalk 2006 简单入门示例程序(附源项目文件下载) 为初学BizTalk Server 2006的开发人员,提供一个简单入门的示例程序,包括一个Receive Port.Send Port ...
- 基于Struts2.3.x+Spring3.2.x+Hibernate4.2.x+EasyUI1.3.4+Maven架构的示例程序
基于Struts2.3.x+Spring3.2.x+Hibernate4.2.x+EasyUI1.3.4+Maven架构的示例程序 不知道为什么,保存的时候显示有一个连接为违禁内容,可能是----. ...
- 如何编译ReactNative示例程序Examples
通过示例程序可以看到一些基本组件的使用,对于学习ReactNative是很有帮助的. 编译示例程序需要将整个项目导入到androidStudio中,androidStudio导入项目时选择react- ...
- ASP.NET AJAX示例程序:实现IDragSource和IDropTarget接口将商品拖放至购物车中
本文来自<ASP.NET AJAX程序设计--第II卷:客户端Microsoft AJAX Library相关>第9章第3节. 9.3 示例程序:实现IDragSource和IDropTa ...
- Linux下的示例程序
linux下的示例程序 #if 0 /* * 1. 遍历目录-1 */ #include <stdio.h> #include <dirent.h> #include &l ...
- python推荐系统-用python写个简单的推荐系统示例程序
用python写个简单的推荐系统示例程序 作者:阿俊 发布于:2011-11-26 16:03 Saturday 分类:推荐系统 python这门语言写程序代码量非常少,短短几行就可以把程序写的很清楚 ...
- OpenCV的示例程序在哪里?
示例程序在各版本的源代码中,以OpenCV – 4.1.2为例,其源代码的下载位置如下: 官网(https://opencv.org/)→Library→Releases→找到OpenCV – 4.1 ...
- MxGraph从入门到精通之1:运行HelloWorld示例程序
简介 MxGraph包括了一个JavaScript编写的客户端软件,以及一系列各个语言写的后端. 客户端是一个图形组件,它需要一个web服务器把相关的文件传给客户端或者直接从本地文件系统运行起来(如下 ...
- [发布]Quartz.NET 示例程序:企业调度器 V0.2 源代码
企业调度器(EntScheduler) V0.1 源代码在Quartz.NET 架构与源代码分析系列 part 3 :Trigger 触发器 已经给出,它实现了基本的作业调度.触发.此次的 V0.2 ...
最新文章
- HDU1599(最小环)
- Ubuntu下安装Chrome浏览器
- sonarqube7.8汉化教程:安装中文插件
- 随机变量的分布函数-定义域问题
- 一些Java面试题深入分析
- C++描述杭电OJ 2010.水仙花数 ||
- 论文浅尝 | 面向多语言语义解析的神经网络框架
- 7、JS面向对象编程之继承
- vSphere Web Client 中的 vSAN 性能图 (2144493)
- [翻译]Chameleon介绍(6) : 动作控件
- ANSYS网格划分简述
- Java 夯实基础之注解
- 尚学堂马士兵struts2操作手册
- Additions HNUST 1713(矩阵快速幂模板 )
- 华为开源平台harmonyos,华为开源平台上线 方舟编译器和鸿蒙系统开源
- 纯CSS制作3D动态相册【流星雨3D旋转相册】HTML+CSS+JavaScriptHTML5七夕情人节表白网页制作
- 实现 等高线图 - 基于python-matplotlib
- 计算机网路课后作业(2)——物理层
- 35K入职华为Java岗那天,我哭了:这5个月做的一切都值了
- 光模块自动测试系统软件,一种用于测试光模块的多通道自动测试方法及系统
热门文章
- Linux 内核C -- 第02课:驱动中的指定初始化
- 杨辉三角Python
- Coins(HDU2844)
- leetcode 877 stone game
- Intellij里面的几个异常处理方案
- InstantiationException in hadoop map reduce program
- 【BFS】迷宫问题c++代码详解(逐句分析)
- java给byte赋值_关于JAVA中Byte数据类型二进制赋值运算报错问题
- xmind各版本区别_XMind各版本功能对比1.PDF
- tomcat后台密码爆破脚本(python+字典)_Web中间件漏洞之Tomcat篇