Windows Phone 7 位图BitmapImage和WriteableBitmap

简介:

继承关系

Object
DependencyObject (abstract)
ImageSource (abstract)
BitmapSource (abstract)
BitmapImage
WriteableBitmap


一、WriteableBitmap 类
提供一个可写入并可更新的 BitmapSource。 BitmapSource 是 Silverlight 图像处理管线的基本构造块,从概念上说表示具有特定大小和分辨率的单个不变的像素集。
命名空间: System.Windows.Media.Imaging
使用 WriteableBitmap 类基于每个框架来更新和呈现位图。 这对于生成算法内容(如分形图像)和数据可视化(如音乐可视化工

具)很有用。

实例:点击程序屏幕不断地获取屏幕图像嵌套进去

 

 

 

 
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" 
  2.               Background="{StaticResource PhoneAccentBrush}"> 
  3.               
  4.             <TextBlock Text="Tap anywhere to capture page" 
  5.                        HorizontalAlignment="Center" 
  6.                        VerticalAlignment="Center" /> 
  7.               
  8.             <Image Name="img" 
  9.                    Stretch="Fill" /> 
  10.         </Grid> 
 
  1. public MainPage()  
  2.         {  
  3.             InitializeComponent();  
  4.         }  
  5.  
  6.         protected override void OnManipulationStarted(ManipulationStartedEventArgs args)  
  7.         {  
  8.             img.Source = new WriteableBitmap(this, null);// 使用element 和 transform来创建一个WriteableBitmap  
  9.  
  10.             args.Complete();  
  11.             args.Handled = true;  
  12.             base.OnManipulationStarted(args);  
  13.         } 

 

public WriteableBitmap(UIElement element, Transform transform) 这个构造函数。element 是要在位图中呈现的元素;transform 是用户在绘制到位图之前最后一步应用到元素的变换。如果您希望位图将元素的变换考虑在内,则这对于您特别有意义。此值可为 null。

这个 WriteableBitmap 构造函数适用于绝大多数的"复制内容"方案。这个构造函数可生成在保留内容基础下,尽量减少空白的 PBGRA32格式(采用32BPP的一种基于sRGB的像素格式)的 WriteableBitmap。它把 element 的各种变化都考虑进去了,这些变化包括:Clip,Effect,Opacity,OpacityMask,Children。当然还有一些变化没有包含,这时候,我们可以对他的父控件进行截屏,就可以扑捉到这些没有包括的变化。另外:WriteableBitmap 不能呈现弹出式控件,如 Popup、ComboBox 和 ToolTip。

二、BitmapImage 类

Silverlight 提供一个经优化以使用可扩展应用程序标记语言 (XAML) 上载图像的专用 BitmapSource。 
命名空间: System.Windows.Media.Imaging
XAML <BitmapImage .../>
BitmapImage 主要支持可扩展应用程序标记语言 (XAML) 语法,并为尚未由 BitmapSource 定义的位图加载引入附加属性。

BitmapImage 实现 ISupportInitialize 接口,以对多个属性的初始化进行优化。 只能在对象初始化过程中进行属性更改。 调用 BeginInit 以表示初始化开始;调用 EndInit 以表示初始化结束。 初始化后,将忽略属性更改。

使用 BitmapImage 构造函数创建的 BitmapImage 对象将自动初始化,且将忽略对属性的更改。

示例

XAML 
<!-- Property Tag XAML Syntax -->
<Image Width="200" Margin="5" Grid.Column="1" Grid.Row="1" >
<Image.Source>
<BitmapImage UriSource="sampleImages/bananas.jpg" />
</Image.Source>
</Image>

实例:分裂图片--通过选择器选择一张照片放进BitmapImage里面然后利用WriteableBitmap将图片分成4块

 

  

 

 
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
  2.             <TextBlock Name="txtblk" 
  3.                        Text="Touch to choose image" 
  4.                        HorizontalAlignment="Center" 
  5.                        VerticalAlignment="Center" /> 
  6.               
  7.             <Grid HorizontalAlignment="Center" 
  8.                   VerticalAlignment="Center"> 
  9.                 <Grid.RowDefinitions> 
  10.                     <RowDefinition Height="*" /> 
  11.                     <RowDefinition Height="*" /> 
  12.                 </Grid.RowDefinitions> 
  13.                   
  14.                 <Grid.ColumnDefinitions> 
  15.                     <ColumnDefinition Width="*" /> 
  16.                     <ColumnDefinition Width="*" /> 
  17.                 </Grid.ColumnDefinitions> 
  18.                   
  19.                 <Image Name="imgUL" Grid.Row="0" Grid.Column="0" Margin="2" /> 
  20.                 <Image Name="imgUR" Grid.Row="0" Grid.Column="1" Margin="2" /> 
  21.                 <Image Name="imgLL" Grid.Row="1" Grid.Column="0" Margin="2" /> 
  22.                 <Image Name="imgLR" Grid.Row="1" Grid.Column="1" Margin="2" /> 
  23.  
  24.             </Grid> 
  25.         </Grid> 

 

 
  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using System.Windows.Input;  
  5. using System.Windows.Media;  
  6. using System.Windows.Media.Imaging;  
  7. using Microsoft.Phone.Controls;  
  8. using Microsoft.Phone.Tasks;  
  9.  
  10. namespace SubdivideBitmap  
  11. {  
  12.     public partial class MainPage : PhoneApplicationPage  
  13.     {  
  14.         PhotoChooserTask photoChooser = new PhotoChooserTask();//创建一个相片选择器  用来选择一张图片  
  15.         
  16.         public MainPage()  
  17.         {  
  18.             InitializeComponent();  
  19.             photoChooser.Completed += OnPhotoChooserCompleted;//添加选择器的完成事件  
  20.         }  
  21.         //点击程序屏幕选择图片  
  22.         protected override void OnManipulationStarted(ManipulationStartedEventArgs args)  
  23.         {  
  24.             int dimension = (int)Math.Min(ContentPanel.ActualWidth,  
  25.                                           ContentPanel.ActualHeight) - 8;  
  26.             //设置选择图片的宽度高度  
  27.             photoChooser.PixelHeight = dimension;  
  28.             photoChooser.PixelWidth = dimension;  
  29.             photoChooser.Show();  
  30.  
  31.             args.Complete();  
  32.             args.Handled = true;  
  33.             base.OnManipulationStarted(args);  
  34.         }  
  35.         //完成选择图片触发的事件  
  36.         void OnPhotoChooserCompleted(object sender, PhotoResult args)  
  37.         {  
  38.             if (args.Error != null || args.ChosenPhoto == null)//没有选择图片则返回跳出该方法  
  39.                 return;  
  40.             //创建BitmapImage来存储选择器的图片  
  41.             BitmapImage bitmapImage = new BitmapImage();  
  42.             bitmapImage.SetSource(args.ChosenPhoto);  
  43.  
  44.             Image imgBase = new Image();  
  45.             imgBase.Source = bitmapImage;  
  46.             imgBase.Stretch = Stretch.None;  
  47.  
  48.             // 左上部分  
  49.             WriteableBitmap writeableBitmap =   
  50.                 new WriteableBitmap(bitmapImage.PixelWidth / 2,  
  51.                                     bitmapImage.PixelHeight / 2);  
  52.             writeableBitmap.Render(imgBase, null);  
  53.             writeableBitmap.Invalidate();  
  54.             imgUL.Source = writeableBitmap;  
  55.  
  56.             //右上部分  
  57.             writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,  
  58.                                                   bitmapImage.PixelHeight / 2);  
  59.             TranslateTransform translate = new TranslateTransform();  
  60.             translate.X = -bitmapImage.PixelWidth / 2;//在绘制到位图中之前应用到元素的变换  
  61.             writeableBitmap.Render(imgBase, translate);//在位图中呈现元素  
  62.             writeableBitmap.Invalidate();//请求重绘整个位图  
  63.             imgUR.Source = writeableBitmap;  
  64.  
  65.             // 左下部分  
  66.             writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,  
  67.                                                   bitmapImage.PixelHeight / 2);  
  68.             translate.X = 0;  
  69.             translate.Y = -bitmapImage.PixelHeight / 2;  
  70.             writeableBitmap.Render(imgBase, translate);  
  71.             writeableBitmap.Invalidate();  
  72.             imgLL.Source = writeableBitmap;  
  73.  
  74.             // 右下部分  
  75.             writeableBitmap = new WriteableBitmap(bitmapImage.PixelWidth / 2,  
  76.                                                   bitmapImage.PixelHeight / 2);  
  77.             translate.X = -bitmapImage.PixelWidth / 2;  
  78.             writeableBitmap.Render(imgBase, translate);  
  79.             writeableBitmap.Invalidate();  
  80.             imgLR.Source = writeableBitmap;  
  81.  
  82.             txtblk.Visibility = Visibility.Collapsed;//隐藏刚开始的文本  
  83.         }  
  84.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078713


相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1249 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
133 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
162 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
230 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
Windows
同样指令bmp.LockBits不同平台(Windows/WinCE)获取位图的数据不同
同样指令bmp.LockBits不同平台(Windows/WinCE)获取位图的数据不同
619 0