Windows 8学习笔记(十一)---图片的显示与保存
图片显示
图片的显示应该都非常熟悉,场景很多,我先把我想的三个列举一下:
场景一:
从本地图片库选择图片进行显示:
async private void LoadPictureByPicker() { FileOpenPicker fileOpen = new FileOpenPicker() { FileTypeFilter={ ".jpg" , ".jpeg" , ".png" , ".bmp" }, ViewMode=PickerViewMode.Thumbnail, SuggestedStartLocation=PickerLocationId.PicturesLibrary }; bitmapImage = new BitmapImage(); storageFile = await fileOpen.PickSingleFileAsync(); if (storageFile != null ) { using (IRandomAccessStream stream = await storageFile.OpenAsync(FileAccessMode.Read)) { bitmapImage.SetSource(stream); } } img.Source = bitmapImage; } |
场景二:
直接请求网络URL
string url = "http://ww4.sinaimg.cn/bmiddle/8193c63ajw1ds3o5e56jaj.jpg";
方法一:
BitmapImage bitmapImage = new BitmapImage( new Uri(url)); img.Source = bitmapImage; |
方法二:
async private void LoadPictrueByUrl() { var rass = RandomAccessStreamReference.CreateFromUri( new Uri(url)); streamRandom = await rass.OpenReadAsync(); var bitmapImage = new BitmapImage(); bitmapImage.SetSource(streamRandom); img.Source = bitmapImage; } |
若是简单的显示图片,用方法一就足够了,若需要保存图片,还是用方法二,因为方法一我目前还没有办法截取到它的流,除非再用HttpRequest请求一次;用方法二相对而言,就方便多了。
场景三:
显示项目文件中的图片
string url = "ms-appx:///images/flower.jpg";
方法一:
BitmapImage bitmapImage = new BitmapImage( new Uri(url)); img.Source = bitmapImage; |
方法二:
async private void LoadPictureByNative() { var rass = RandomAccessStreamReference.CreateFromUri( new Uri(url)); streamRandom = await rass.OpenReadAsync(); var bitmapImage = new BitmapImage(); bitmapImage.SetSource(streamRandom); img.Source = bitmapImage; } |
原理与场景二是类似的
保存图片
保存图片也分两种情况
直接保存到图片库
StorageFolder folder = KnownFolders.PicturesLibrary; storageFile = await folder.CreateFileAsync( "sample.jpg" , CreationCollisionOption.ReplaceExisting); IBuffer buffer = RandomAccessStreamToBuffer(streamRandom); await FileIO.WriteBufferAsync(storageFile, buffer); |
RandomAccessStreamToBuffer是自定的转换方法,在前面流的转换中也已写了,这里再写一下吧..
private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream) { Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0)); MemoryStream memoryStream = new MemoryStream(); if (stream != null ) { byte [] bytes = ConvertStreamTobyte(stream); if (bytes != null ) { var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(bytes); } } IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, ( int )memoryStream.Length); return buffer; } public static byte [] ConvertStreamTobyte(Stream input) { byte [] buffer = new byte [16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } |
显示保存文件对话框,可以自行选择保存路径,这时需用到FileSavePicker
FileSavePicker fileSave = new FileSavePicker(); fileSave.SuggestedSaveFile = storageFile; fileSave.SuggestedStartLocation = PickerLocationId.Desktop; fileSave.SuggestedFileName = "test418" ; fileSave.DefaultFileExtension = ".jpg" ; fileSave.FileTypeChoices.Add( "JPEG file" , new List< string > { ".jpg" }); StorageFile file = await fileSave.PickSaveFileAsync();</ string > |
可能我上面的方法不是很简单方便,希望有更简捷的同胞可以共同分享下哈~
Trackback:
http://www.cnblogs.com/jing870812/archive/2012/04/19/2457805.html