-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainPage.xaml.cs
236 lines (203 loc) · 6.63 KB
/
MainPage.xaml.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
namespace GoodYeyGenerator
{
public partial class MainPage : PhoneApplicationPage
{
WriteableBitmap originBitmap;
WriteableBitmap editedBitmap;
ApplicationBarIconButton fileChooseButton;
ApplicationBarIconButton fileSaveButton;
ApplicationBarMenuItem aboutItem;
// コンストラクター
public MainPage()
{
InitializeComponent();
// ApplicationBarItemを追加
this.fileChooseButton = new ApplicationBarIconButton
{
IconUri = new Uri("/icons/appbar.folder.rest.png", UriKind.Relative),
Text = "読み込み"
};
this.fileSaveButton = new ApplicationBarIconButton
{
IconUri = new Uri("/icons/appbar.save.rest.png", UriKind.Relative),
Text = "保存",
IsEnabled = false
};
this.aboutItem = new ApplicationBarMenuItem
{
Text = "バージョン情報"
};
this.ApplicationBar.Buttons.Add(this.fileChooseButton);
this.ApplicationBar.Buttons.Add(this.fileSaveButton);
this.ApplicationBar.MenuItems.Add(this.aboutItem);
this.fileChooseButton.Click += new EventHandler(fileChooseButton_Click);
this.fileSaveButton.Click += new EventHandler(fileSaveButton_Click);
this.aboutItem.Click += new EventHandler(aboutItem_Click);
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (this.pictureImage.Source == null)
{
this.startMessageShow.Begin();
}
else
{
this.startMessageGrid.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void fileChooseButton_Click(object sender, EventArgs e)
{
// 画像を選ぶ
var task = new PhotoChooserTask
{
PixelHeight = 800,
PixelWidth = 600,
ShowCamera = true
};
task.Completed += (ps, pe) =>
{
if (pe.TaskResult == TaskResult.OK && pe.Error == null)
{
this.originBitmap = ConvertToWriteableBitmap(pe.ChosenPhoto);
this.editedBitmap = ApplyYey(Grayscare(this.originBitmap));
this.pictureImage.Source = editedBitmap.ToBitmapImage();
this.fileSaveButton.IsEnabled = true;
this.startMessageGrid.Visibility = System.Windows.Visibility.Collapsed;
}
else if (pe.TaskResult == TaskResult.Cancel && pe.Error != null)
{
MessageBox.Show(
"PCでZune Softwareと同期中の可能性があります。\n" +
"Zune Softwareを終了するか、端末をPCから切断してもう一度実行して下さい。",
"エラー",
MessageBoxButton.OK);
}
};
task.Show();
}
private void fileSaveButton_Click(object sender, EventArgs e)
{
this.editedBitmap.SaveToMediaLibrary(DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".jpg");
MessageBox.Show("保存しました。");
}
void aboutItem_Click(object sender, EventArgs e)
{
this.NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
}
/// <summary>
/// ストリームを WriteableBitmap に変換します。
/// </summary>
/// <param name="st"></param>
/// <returns></returns>
private WriteableBitmap ConvertToWriteableBitmap(Stream st)
{
using (var s = PictureExtension.PhotoStreamToMemoryStream(st))
{
try
{
var size = PictureExtension.GetOriginalSize(s);
WriteableBitmap bitmap = null;
bitmap = new WriteableBitmap((int)size.Width, (int)size.Height);
// WriteableBitmapに画像を読み込ませる
s.Seek(0, SeekOrigin.Begin);
bitmap.LoadJpeg(s);
return bitmap;
}
catch
{
return null;
}
}
}
/// <summary>
/// 画像をグレースケールにします。
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private WriteableBitmap Grayscare(WriteableBitmap bitmap)
{
var edit = bitmap.Clone();
edit.ToGrayScale();
return edit;
//using (var s = PictureExtension.PhotoStreamToMemoryStream(bitmap))
//{
// var size = PictureExtension.GetOriginalSize(s);
// WriteableBitmap edit = null;
// this.originBitmap = new WriteableBitmap((int)size.Width, (int)size.Height);
// edit = new WriteableBitmap((int)size.Width, (int)size.Height);
// // WriteableBitmapに画像を読み込ませる
// s.Seek(0, SeekOrigin.Begin);
// this.originBitmap.LoadJpeg(s);
// // editableの方にも同じのをコピー
// edit = this.originBitmap.Clone();
// // グレスケ化
// edit = this.originBitmap.ToGrayScale();
// return edit;
//}
}
/// <summary>
/// 画像に遺影を重ねます。
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
private WriteableBitmap ApplyYey(WriteableBitmap bitmap)
{
var wb = bitmap.Clone();
var file = Application.GetResourceStream(new Uri("photoframe.png", UriKind.Relative));
var yey = new WriteableBitmap(wb.PixelWidth, wb.PixelHeight);
yey.LoadJpeg(file.Stream);
for (int i = 0; i < yey.Pixels.Length; i++)
{
var oPx = yey.Pixels[i];
var a2 = Convert.ToByte((oPx >> 24) & 0xff);
var ax2 = (double)a2 / 255;
if (a2 == 0)
{
// 透明なので下地を描画
}
else if (a2 == 255)
{
// 不透明なので上に書き込み
wb.Pixels[i] = yey.Pixels[i];
}
else
{
// 下地の画像の色を取り出す
var a = Convert.ToByte((wb.Pixels[i] >> 24) & 0xff);
var r = Convert.ToByte((wb.Pixels[i] >> 16) & 0xff);
var g = Convert.ToByte((wb.Pixels[i] >> 8) & 0xff);
var b = Convert.ToByte((wb.Pixels[i]) & 0xff);
// 枠の色を取り出す
var r2 = Convert.ToByte((oPx >> 16) & 0xff);
var g2 = Convert.ToByte((oPx >> 8) & 0xff);
var b2 = Convert.ToByte((oPx) & 0xff);
// アルファ値を元に合成
var a3 = Convert.ToByte((a * (1.0 - ax2) + a2 * ax2));
var r3 = Convert.ToByte((r * (1.0 - ax2) + r2 * ax2));
var g3 = Convert.ToByte((g * (1.0 - ax2) + g2 * ax2));
var b3 = Convert.ToByte((b * (1.0 - ax2) + b2 * ax2));
wb.Pixels[i] = a3 << 24 | r3 << 16 | g3 << 8 | b3;
}
}
wb.Invalidate();
return wb;
}
}
}