windows store app 读写图片
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Graphics.Imaging;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using System.IO;
using Windows.Foundation;
using System.Collections.Generic; namespace Common
{
public class ImageHelper
{
public const int THumbLength = ;
public static Size MinSizeSupported = new Size(, );
public static Size MaxSizeSupported = new Size(, ); static Guid EncoderIDFromFileExtension(string strExtension)
{
Guid encoderId;
switch (strExtension.ToLower())
{
case ".jpg":
case ".jpeg":
encoderId = BitmapEncoder.JpegEncoderId;
break;
case ".bmp":
encoderId = BitmapEncoder.BmpEncoderId;
break;
case ".png":
default:
encoderId = BitmapEncoder.PngEncoderId;
break;
}
return encoderId;
}
static Guid DecoderIDFromFileExtension(string strExtension)
{
Guid encoderId;
switch (strExtension.ToLower())
{
case ".jpg":
case ".jpeg":
encoderId = BitmapDecoder.JpegDecoderId;
break;
case ".bmp":
encoderId = BitmapDecoder.BmpDecoderId;
break;
case ".png":
default:
encoderId = BitmapDecoder.PngDecoderId;
break;
}
return encoderId;
} public async static Task<WriteableBitmap> ReadBitmap(IRandomAccessStream fileStream, string type)
{
WriteableBitmap bitmap = null;
try
{
Guid decoderId = DecoderIDFromFileExtension(type); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(decoderId, fileStream);
BitmapTransform tf = new BitmapTransform(); uint width = decoder.OrientedPixelWidth;
uint height = decoder.OrientedPixelHeight;
double dScale = ; if (decoder.OrientedPixelWidth > MaxSizeSupported.Width || decoder.OrientedPixelHeight > MaxSizeSupported.Height)
{
dScale = Math.Min(MaxSizeSupported.Width / decoder.OrientedPixelWidth, MaxSizeSupported.Height/decoder.OrientedPixelHeight );
width =(uint)( decoder.OrientedPixelWidth * dScale);
height = (uint)(decoder.OrientedPixelHeight * dScale); tf.ScaledWidth = (uint)(decoder.PixelWidth* dScale);
tf.ScaledHeight = (uint)(decoder.PixelHeight* dScale);
} bitmap = new WriteableBitmap((int)width, (int)height); PixelDataProvider dataprovider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, tf,
ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
byte[] pixels = dataprovider.DetachPixelData(); Stream pixelStream2 = bitmap.PixelBuffer.AsStream(); pixelStream2.Write(pixels, , pixels.Length);
//bitmap.SetSource(fileStream);
}
catch
{
} return bitmap;
} public async static Task<WriteableBitmap> ReadBitmap(StorageFile file)
{
IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
WriteableBitmap bitmap = await ReadBitmap(stream, file.FileType.ToLower()); return bitmap;
} public async static Task<bool> SaveBitmap(IRandomAccessStream stream, Guid encoderId, WriteableBitmap bitmap, BitmapTransform bitmapTransform)
{
bool bSuccess = true;
try
{
Stream pixelStream = bitmap.PixelBuffer.AsStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
if (bitmapTransform != null)
{
encoder.BitmapTransform.ScaledWidth = bitmapTransform.ScaledWidth;
encoder.BitmapTransform.ScaledHeight = bitmapTransform.ScaledHeight;
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
encoder.BitmapTransform.Bounds = bitmapTransform.Bounds;
} byte[] pixels = new byte[pixelStream.Length]; pixelStream.Read(pixels, , pixels.Length); encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
(uint)bitmap.PixelWidth,
(uint)bitmap.PixelHeight,
, // Horizontal DPI
, // Vertical DPI
pixels
);
await encoder.FlushAsync(); pixelStream.Dispose();
}
catch
{
bSuccess = false;
} return bSuccess;
} async static Task<bool> SaveBitmap(StorageFile file, WriteableBitmap bitmap, BitmapTransform bitmapTransform)
{
bool bSuccess = true;
try
{
// Application now has read/write access to the saved file
Guid encoderId = EncoderIDFromFileExtension(file.FileType); IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); bSuccess = await SaveBitmap(fileStream, encoderId,bitmap,bitmapTransform); fileStream.Dispose(); }
catch
{
bSuccess = false;
} return bSuccess;
} public async static Task<bool> SaveBitmap(StorageFile file, WriteableBitmap bitmap)
{
bool bSuccess = await SaveBitmap(file, bitmap, null);
return bSuccess;
} public async static Task<bool> SaveBitmapWithFilePicker(WriteableBitmap bitmap)
{
FileSavePicker save = new FileSavePicker();
save.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
save.DefaultFileExtension = ".jpg";
save.SuggestedFileName = "new image";
save.FileTypeChoices.Add(".bmp", new List<string>() { ".bmp" });
save.FileTypeChoices.Add(".png", new List<string>() { ".png" });
save.FileTypeChoices.Add(".jpg", new List<string>() { ".jpg", ".jpeg" }); StorageFile savedItem = await save.PickSaveFileAsync();
if (savedItem == null)
{
return false;
} return await SaveBitmap(savedItem, bitmap);
} public async static Task<bool> SaveBitmapAsThumb(StorageFile file, WriteableBitmap bitmap, double width, double height)
{
BitmapTransform transform = new BitmapTransform(); double ratio = Math.Min(width / bitmap.PixelWidth, height / bitmap.PixelHeight); transform.ScaledWidth = (uint)(bitmap.PixelWidth * ratio);
transform.ScaledHeight = (uint)(bitmap.PixelHeight * ratio);
transform.InterpolationMode = BitmapInterpolationMode.Fant; return await SaveBitmap(file, bitmap, transform);
}
public async static Task<bool> SaveBitmapAsThumb(StorageFile file, WriteableBitmap bitmap, Windows.Foundation.Rect cropRect)
{
BitmapTransform transform = new BitmapTransform(); double ratio =THumbLength/ Math.Max(cropRect.Width, cropRect.Height ); transform.ScaledWidth = (uint)(bitmap.PixelWidth * ratio);
transform.ScaledHeight = (uint)(bitmap.PixelHeight * ratio);
transform.Bounds = new BitmapBounds()
{
X = (uint)(cropRect.X * ratio),
Y = (uint)(cropRect.Y * ratio),
Width = (uint)(cropRect.Width * ratio),
Height = (uint)(cropRect.Height * ratio)
};
transform.InterpolationMode = BitmapInterpolationMode.Fant; return await SaveBitmap(file, bitmap, transform);
}
}
}
windows store app 读写图片的更多相关文章
- 05、Windows Store app 的图片裁切(更新)
在 Win Phone Silverlight api 中,有一个 PhotoChooserTask 选择器,指定宽.高属性,在选择图片的时候, 可以进行裁切,代码: PhotoChooserTask ...
- 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)
这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...
- Windows Store App 过渡动画
Windows Store App 过渡动画 在开发Windows应用商店应用程序时,如果希望界面元素进入或者离开屏幕时显得自然和流畅,可以为其添加过渡动画.过渡动画能够及时地提示用户屏幕所发 ...
- Windows store app[Part 4]:深入WinRT的异步机制
接上篇Windows store app[Part 3]:认识WinRT的异步机制 WinRT异步机制回顾: IAsyncInfo接口:WinRT下异步功能的核心,该接口提供所有异步操作的基本功能,如 ...
- Windows store app[Part 3]:认识WinRT的异步机制
WinRT异步机制的诞生背景 当编写一个触控应用程序时,执行一个耗时函数,并通知UI更新,我们希望所有的交互过程都可以做出快速的反应.流畅的操作感变的十分重要. 在连接外部程序接口获取数据,操作本地数 ...
- 01、Windows Store APP 设置页面横竖屏的方法
在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是 ...
- Windows store app[Part 1]:读取U盘数据
Windows 8系统下开发App程序,对于.NET程序员来说,需要重新熟悉下类库. 关于WinRT,引用一张网上传的很多的结构图: 图1 针对App的开发,App工作在系统划定的安全沙箱内,所以通过 ...
- Windows Store App 偏移特效
通过前面讲解的内容,读者已经了解了如何在三维空间中使旋转对象绕指定的旋转中心旋转一定的角度.接下来在这个基础上进一步讲解如何对旋转对象进行平移.下面首先介绍一下用到的几个属性. q LocalOff ...
- Windows Store App 旋转中心
旋转中心的位置可以通过设置CenterOfRotationX.CenterOfRotationY和CenterOfRotationZ属性来指定.CenterOfRotationX和CenterOfRo ...
随机推荐
- eclipse中的tomcat debug模式启动报超时45s
在eclipse中加入tomcat,开debug模式非常好用,这里就不介绍了 最近eclipse的tomcat开debug模式就是启动不了,增加时间也不能解决,但是非debug模式就可以打开,我觉得是 ...
- LeetCode24 Swap Nodes in Pairs
题意: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 升级树莓派archlinux系统到新sd卡
由于之前把树莓派系统安装在4gb的sd卡上,随着系统的更新及安装了一大堆软件包之后,系统提示空间不足了.网上搜索了下,把所有数据迁移到新的sd卡上还是比较简单的. 克隆sd卡: 1,关闭树莓派电源,取 ...
- [Javascript] Try...Catch和异常处理
在JavaScript可以使用try...catch来进行异常处理.例如: try { foo.bar(); } catch (e) { alert(e.name ...
- Laravel自学第一课:laravel下载与安装
本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...
- Js 实现五级联动
js实现多级联动的方法很多,这里给出⼀种5级联动的例子,其实可以扩展成N级联动,在做项目的时候碰到了这样⼀个问题但是有不能从数据库中动态的加载这些选项,所以只有想办法从单个的页面着手来处理了,应为项目 ...
- Android 自定义Dialog工具类
由于项目的需要,系统的弹出框已经不能满足我们的需求,我们需要各式各样的弹出框,这时就需要我们去自定义弹出框了. 新建布局文件 dialog_layout.xml,将下面内容复制进去 <?xml ...
- mybatis--MapperScannerConfigurer
一般我们这样配置 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryB ...
- Freebsd 编译内核
# cd /usr/src/sys/i386/conf # cp GENERIC GENERIC.20060812# ee GENERIC 如果要加入ipf防火墙的话则加入options ...
- hdu 4144 状态压缩dp
#include<map> #include<set> #include<cmath> #include<queue> #include<cstd ...