Windows 8 learning notes (11)-display and save images

Source: Internet
Author: User

Image Display

Image display should be very familiar with many scenarios. Let me first list the three items I want:

Scenario 1:

Select an image from the local image library for display:

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;        }

 

Scenario 2:

Directly request the network URL

String url = "http://ww4.sinaimg.cn/bmiddle/8193c63ajw1ds3o5e56jaj.jpg ";

Method 1:

BitmapImage bitmapImage = new BitmapImage(new Uri(url));img.Source = bitmapImage;

Method 2:

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;        }

If you simply display an image, it is enough to use method 1. If you need to save the image, use method 2, because method 1 has no way to capture the stream, unless the httprequest request is used again, it is much more convenient to use method 2.

Scenario 3:

Show images in the project file

String url = "MS-appx: // images/flower.jpg ";

Method 1:

BitmapImage bitmapImage = new BitmapImage(new Uri(url));img.Source = bitmapImage;

Method 2:

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;        }

The principle is similar to scenario 2.

Save image

There are two ways to save an image:

Save to image library directly

StorageFolder folder = KnownFolders.PicturesLibrary;storageFile = await folder.CreateFileAsync("sample.jpg", CreationCollisionOption.ReplaceExisting);IBuffer buffer = RandomAccessStreamToBuffer(streamRandom);await FileIO.WriteBufferAsync(storageFile, buffer);

Randomaccessstreamtobuffer is a custom conversion method. It has already been written in the previous Stream Conversion. Please write it again here ..

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();            }        }

 

The Save file dialog box is displayed. You can select a save path.

FileSavePicker fileSave = new FileSavePicker();fileSave.SuggestedSaveFile = storageFile;fileSave.SuggestedStartLocation = PickerLocationId.Desktop;fileSave.SuggestedFileName = "test418";fileSave.DefaultFileExtension = ".jpg";fileSave.FileTypeChoices.Add("JPEG file", new List
 
   { ".jpg" });StorageFile file = await fileSave.PickSaveFileAsync();
 

 

Maybe the method above is not very simple and convenient. I hope more simple people can share it with me ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.