-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageViewPage.xaml.cs
More file actions
105 lines (97 loc) · 3.56 KB
/
ImageViewPage.xaml.cs
File metadata and controls
105 lines (97 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using NGopher.Gopher;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace NGopher
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ImageViewPage : Page, IFileSavePickerContinuable
{
private GopherClient _gopher;
private string _selector;
private byte[] _content;
private BitmapImage _image;
public ImageViewPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter == null) return;
var server = e.Parameter as string;
var x = server.Split('\x01');
var y = x[1].Split('\x02');
_gopher = new GopherClient
{
Server = x[0],
Port = Convert.ToUInt16(y[0])
};
_selector = y[1];
SelectorTextBlock.Text = _selector.ToUpper();
_content = await _gopher.MakeBinaryRequest(_selector);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(_content);
writer.StoreAsync().GetResults();
}
_image = new BitmapImage();
await _image.SetSourceAsync(ms);
ContentImage.Source = _image;
}
}
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var fn = _selector.Split('/').Last();
var fsp = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
SuggestedFileName = fn
};
fsp.FileTypeChoices.Add("Image file", new List<string> { ".gif", ".png", ".jpg", ".jpeg", ".bmp" });
fsp.PickSaveFileAndContinue();
}
public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
StorageFile file = args.File;
if (file == null) return; // saving cancelled
CachedFileManager.DeferUpdates(file);
await FileIO.WriteBytesAsync(file, _content); // save _content to file
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
// successfully saved file
}
else
{
// oh no
}
}
}
}