Windows Phone 7 Bitmap编码
Windows Phone 7为图片的编码解码提供了高度灵活的、托管的API。方法如下:
1.利用WriteableBitmap类更新并渲染已有的bitmap(除非它是protected的)
2.利用Extensions.SaveJpeg方法将WriteableBitmap对象编码成一个JPEG流
该方法带有设置目标JPEG文件宽度和高度的参数,其语法如下:
public static void SaveJpeg(
this WriteableBitmap bitmap,
Stream targetStream,
int targetWidth,
int targetHeight,
int orientation,
int quality
)
下面是一个在WP7中将image编码解码的例子:
· 将WriteableBitmap对象编码成一个JPEG流,并将其添加到Picture Library中
· 将JPEG流解码成一个WriteableBitmap对象
首先,创建一个Windows Phone Application,在MainPage.xaml里添加一个Image控件。
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="img" Height="300" Stretch="None"/>
</StackPanel>
我们在按钮点击事件中将一个JPEG型图片解码成WriteableBitmap对象,
<Button x:Name="btnLoad" Content="Load Image" Click="btnLoad_Click"/>
后台代码如下:
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
//为JPEG文件创建一个流
StreamResourceInfo sri = null;
//注意,此时应将f1.jpg的Build Action设为Content
Uri jpegUri = new Uri("fl.jpg", UriKind.Relative);
sri = Application.GetResourceStream(jpegUri);
//将该流解码
WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(sri.Stream);
img.Source = bitmap;
}
下面,我们将WriteableBitmap对象编码成一个JPEG流。
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image x:Name="img" Height="300" Stretch="None"/>
<Button x:Name="btnLoad" Content="Load Image" Click="btnLoad_Click"/>
<Button x:Name="btnSave" Content="Save Image" Click="btnSave_Click"/>
</StackPanel>
以下代码是将图片保存到Picture Library中:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// 在独立存储中为JPEG文件创建一个文件名。
String tempJPEG = "fl.jpg";
// 创建虚拟存储和文件流,确保tempJPEG唯一。
var store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists(tempJPEG))
{
store.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = store.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri("fl.jpg", UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
//将 WriteableBitmap 对象编码成JPEG 流。
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
// 从独立存储中创建一个新的流,并将该JPEG文件存储到Windows Phone的媒体库中。
fileStream = store.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);
MediaLibrary mediaLibrary = new MediaLibrary();
Picture pic = mediaLibrary.SavePicture("savedflimage.jpg", fileStream);
fileStream.Close();
}
注意:为了使用MediaLibrary,我们需要添加Microsoft.XNA.Framework引用。
这里,为了清楚查看我们所保存的JPEG文件,使用PhotoChooserTask:
private void btnPhotoChooser_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Show();
}
Windows Phone 7 Bitmap编码相关推荐
- Windows - CMD窗口UTF8编码乱码问题的解决!
Windows - CMD窗口UTF8编码乱码问题的解决! 参考文章: (1)Windows - CMD窗口UTF8编码乱码问题的解决! (2)https://www.cnblogs.com/suny ...
- 〖Windows〗Linux的Qt程序源码转换至Windows平台运行,编码的解决
在中国大陆,Windows默认的编码是gb2312,而Linux是UTF8: 多数情况下,把Linux上的程序转换至Windows上运行需要进行编码转换才能正常显示: 而其实大可以不必的,同样,文件使 ...
- 把windows系统的默认编码改成UTF-8
对于开发人员来说windows的编码的确是神一般的存在,windows老版本语言编码实际上是ascii和gbk编码混用,因此经常乱码.现在UTF-8国际化流行的阶段,windows这么多年,不知道耽误 ...
- windows下统一mysql编码_mysql5.7 windows7编码统一utf-8
查看mysql数据编码 登录mysql服务,查看mysql数据库默认的编码 mysql> show variables like 'character%'; +----------------- ...
- Windows 下修改 MySQL 编码为 utf8
问题 Windows 下安装 MySQL 后,默认编码不全utf8. mysql> show variables like '%char%'; +------------------------ ...
- 内核下断链隐藏进程(兼容多版本Windows系统,非硬编码)
前言 在Windows内核下,我们可以通过获取EPROCESS结构来隐藏进程,EPROCESS是微软未公开的结构体,其本质上就是双向链表的一个节点,通过修改该双向链表即可达到隐藏进程的目的.但EPRO ...
- 3分钟教你图解Bitmap编码传输
在Android的图片传输的流程为: Created with Raphaël 2.1.2Bitmap二进制流Byte数组Base64编码String进行传输Base64解码还原为原来的Byte数组, ...
- Windows编程01_应用程序分类,开发工具和库,第一个Windows程序,字符编码
01 Windows应用程序分类 1.应用程序分类 控制台程序Console DOS程序,本身没有窗口,通过Windows DOS窗口执行(借的的操作系统的窗口) 窗口程序 拥有自己的窗口,可以与用户 ...
- Windows输出Apple ProRes编码视频
本文不定期更新,最新内容请访问:https://www.shuaiqi.me/2018/08/20/windowsshuchuapple-proresbianmashipin/ 2019.02.13 ...
最新文章
- js自定义函数及参数问题
- Win7下安装一个装逼文本编辑器Atom + activate-power-mode插件
- 5分钟上手写ECharts的第一个图表
- 测量一组对角矩阵的频率和质量
- python-面向对象编程设计与开发
- LOJ 2085: 洛谷 P1587: bzoj 4652: 「NOI2016」循环之美
- .NET在VS2008中生成DLL并调用
- html5编辑漂亮静态页面工具_青岛HTML5与Web前端
- linux 文件安装大全,linux 文件包安装大全
- 一网打尽:14种预训练语言模型大汇总
- 学习LoadRunner之C语言函数
- 使用highEcharts做出中国地图的效果图
- Nagios常见问题集锦
- Matlab中pickic_高颜值甜品DIY |春季甜品Picnic野餐系列,一起过个惬意慵懒的午后时光❗️...
- c atol()函数_atol()函数以及C ++中的示例
- tensorflow使用GPU加速
- 云计算的三国演义!华为云、阿里云、腾讯云B端市场策略全解读
- flowable 会签和或签的实现 任务多实例
- 为什么你学过Java却忘光了——记第一次助教同学见面会
- 三款win7远程桌面管理工具介绍