惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Y
Y Combinator Blog
F
Full Disclosure
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
S
SegmentFault 最新的问题
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
GbyAI
GbyAI
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
L
LangChain Blog
T
Tenable Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog

博客园 - my favorite

【转】QT样式表 (QStyleSheet) 【转】Qt事件循环与线程 二 [转]Qt 智能指针学习 【转】Android Web Server 【转】Android Http Server Java Annotation 注释语法 android中json的序列化与反序列化 JavaScript定义类的几种方式 js获取浏览器高度和宽度值(多浏览器) Setting up SSL made easy… Better, Faster, Easier SSL testing for ASP.NET MVC & WebForms ASP.NET MVC 3 Internationalization Windows 8 学习笔记(十四)--.map文件与.kml文件的解析 Windows 8学习笔记(十二)--集合控件 Windows 8 学习笔记(十三)--生命周期 Windows 8 Metro App学习笔记(九)—磁砖 Windows 8学习笔记(十)----Notification Windows 8 学习笔记(八)--各种流之间的转换 Windows 8学习笔记(七)--Input输入设备
Windows 8学习笔记(十一)---图片的显示与保存
my favorite · 2012-07-03 · via 博客园 - my favorite

图片显示

图片的显示应该都非常熟悉,场景很多,我先把我想的三个列举一下:

场景一:

从本地图片库选择图片进行显示:

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