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 ...
随机推荐
- JqGrid TreeView使用
1.前端 <script src="@Url.Content("~/Scripts/jquery/jquery-1.9.0.min.js")" type= ...
- InAction-根据LBS数据手机用户移动轨迹
看了以后学了不少通信运营商关于用户数据记录的知识啊. 本来想从网上找真实数据集的,但是网上的数据不合这个DEMO的场景要求,于是用作者提供的python脚本生成一定数据量的数据来实践(当然,这些数据结 ...
- .net对各表的操作详细到字段的更改记录的日志
存入数据库中,目前的字段包括操作人,操作时间,sql语句,被修改的字段,字段原值,操作人的身份. /// <summary> /// 添加操作日志 /// </summary> ...
- vb6.0如何让窗体跟随鼠标运动
首先将form的boderstyle属性设为0 Dim movesScreen As Boolean Dim mousX As Integer Dim mousY As Integer Dim cur ...
- 看完final的感受
今天没课,(其实是有体育课的,去打了一会球就跑路了...)就在宿舍看world final ; 我去,老毛子真是好厉害,看的我目瞪口呆,哈喇子直流; 上交的大神好厉害,本来还以为上交要夺冠的,最后罚时 ...
- hdu 4115 2-SAT判定
思路:将每个回合的平手和赢最为一对对立状态.那么后面就是2-SAT判断了. #include<iostream> #include<cstdio> #include<al ...
- 转:android surface简单使用Demo
转: http://blog.csdn.net/listening_music/article/details/6860786 通过之前介绍的如何自定义View, 我们知道使用它可以做一些简单的动画效 ...
- asp.net中三层架构与mvc之区别?
对于标题中的问题,如果是没有同时接触三层架构和mvc的初级.net开发人员,想必一定会非常糊涂和混淆.关于此我也百度过N回,看过N多帖子和 回答,但几乎没有人能表述清楚.近期我从典型mvc+entit ...
- Python之随机数
import numpy as np list = range(3) # 0 1 2 np.random.shuffle(list)#2 1 3,打乱了list import randomprint ...
- PHP与MySQL中编码的设置
php代码 header("Content-type:text/html;Charset=utf8"); myql_query("set names utf8" ...