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 ...
随机推荐
- AllJoyn Bundled Daemon 使用方式研究
关于AllJoyn不多做介绍,请看官网:www.alljoyn.org/ 0. 问题来源: 应用程序要使用AllJoyn库,就必须启动deamon. 目前有两种方式: 使用standalone形式,单 ...
- Javascript Date原型方法
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- Unity3D 使用 UI 的 Grid Layout Group 组件。
1.首先创建一个容器,用于存放列表项的内容. 这里使用 Panel 来做为容器. 这里要注意! “Grid Layout Group”是要增加在容器的游戏对象里. 同时,只有容器对象的子对象才有排列效 ...
- 菜菜菜鸟学习之JavaWeb 入门1(自己的学习理解,不对之处请大神们多多指教啊)
一.相关基础知识 1.C/S(Client/Server)架构和B/S(Browser/Server)架构 首先说C/S架构,简单讲其实很常见,类似QQ等需要下载客户端的应用程序就是建立在C/S架构中 ...
- java jdbc连接mysql
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- JAVA 多态的一种实现
今天一个同事问我一个问题,就是关于子类,父类之间方法的调用这里的.这里我整理了一个小DEMO. 代码如下: 父类的代码: public abstract class ClassA { public f ...
- OpenShare常见问题及解答
OpenShare常见问题及回答: Q:OpenShare可以整合SAP么? A:当然可以,OpenShare是真正完全开放的产品,但要进行二次开发,事实上我们帮我们大部分的客户都整合了SAP,包括数 ...
- .Net简单上传与下载
上传: 首先上传我们需要一个控件-FileUpLoad: 再加上一个上传按钮: 在上传按钮的Click事件中添加如下代码: FileUpload1.SaveAs(Server.MapPath(&quo ...
- (转载)一步一步学Linq to sql系列文章
现在Linq to sql的资料还不是很多,本人水平有限,如果有错或者误导请指出,谢谢. 一步一步学Linq to sql(一):预备知识 一步一步学Linq to sql(二):DataContex ...
- CSS之Generator
这个工具可以,收藏一下.CSS Generator