markus / KCOM / Common / ImageSourceHelper.cs @ 92442e4a
이력 | 보기 | 이력해설 | 다운로드 (1.98 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Windows.Media; |
7 |
using System.Windows.Media.Imaging; |
8 |
|
9 |
namespace KCOM.Common |
10 |
{ |
11 |
public static class ImageSourceHelper |
12 |
{ |
13 |
public static async System.Threading.Tasks.Task<BitmapImage> GetDownloadImageAsync(Uri uri, double pageWidth, double pageHeight) |
14 |
{ |
15 |
System.Net.Http.HttpClient httpClient = null; |
16 |
System.IO.Stream responseStream = null; |
17 |
System.IO.MemoryStream memoryStream = null; |
18 |
BitmapImage image = null; |
19 |
|
20 |
try |
21 |
{ |
22 |
using (httpClient = new System.Net.Http.HttpClient()) |
23 |
{ |
24 |
using (responseStream = await httpClient.GetStreamAsync(uri)) |
25 |
{ |
26 |
using (memoryStream = new System.IO.MemoryStream()) |
27 |
{ |
28 |
System.Diagnostics.Debug.WriteLine("이미지 다운로드"); |
29 |
|
30 |
await responseStream.CopyToAsync(memoryStream); |
31 |
|
32 |
image = new BitmapImage(); |
33 |
image.BeginInit(); |
34 |
image.CacheOption = BitmapCacheOption.OnLoad; |
35 |
image.StreamSource = memoryStream; |
36 |
image.DecodePixelWidth = (int)pageWidth; |
37 |
image.DecodePixelHeight = (int)pageHeight; |
38 |
image.EndInit(); |
39 |
image.Freeze(); |
40 |
return image; |
41 |
} |
42 |
} |
43 |
} |
44 |
} |
45 |
catch (Exception ex) |
46 |
{ |
47 |
throw new Exception("Image Load Error ", ex); |
48 |
} |
49 |
finally |
50 |
{ |
51 |
httpClient?.Dispose(); |
52 |
responseStream?.Dispose(); |
53 |
memoryStream?.Dispose(); |
54 |
image = null; |
55 |
} |
56 |
} |
57 |
} |
58 |
} |