markus / KCOM / Common / ImageSourceHelper.cs @ 82fd7a58
이력 | 보기 | 이력해설 | 다운로드 (6.8 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 |
public static byte[] CopyPixels(BitmapSource image) |
59 |
{ |
60 |
byte[] result = new byte[image.PixelWidth * image.PixelHeight * 4]; |
61 |
image.CopyPixels(result, image.PixelWidth * 4, 0); |
62 |
|
63 |
return result; |
64 |
} |
65 |
|
66 |
public static byte[] SetBrightness(int brightness, byte[] pixels) |
67 |
{ |
68 |
for (int i = 0; i < pixels.Length / 4; ++i) |
69 |
{ |
70 |
byte b = pixels[i * 4]; |
71 |
byte g = pixels[i * 4 + 1]; |
72 |
byte r = pixels[i * 4 + 2]; |
73 |
byte a = pixels[i * 4 + 3]; |
74 |
|
75 |
if (r != 255 || g != 255 || b != 255 || a == 255) |
76 |
{ |
77 |
b = (byte)(b + brightness); |
78 |
g = (byte)(g + brightness); |
79 |
r = (byte)(r + brightness); |
80 |
///a = (byte)(a + brightness); |
81 |
|
82 |
if (r < 0) r = 1; |
83 |
if (r > 255) r = 255; |
84 |
|
85 |
if (g < 0) g = 1; |
86 |
if (g > 255) g = 255; |
87 |
|
88 |
if (b < 0) b = 1; |
89 |
if (b > 255) b = 255; |
90 |
|
91 |
pixels[i * 4] = b; |
92 |
pixels[i * 4 + 1] = g; |
93 |
pixels[i * 4 + 2] = r; |
94 |
} |
95 |
} |
96 |
|
97 |
return pixels; |
98 |
} |
99 |
|
100 |
public static byte[] SetBrightnessAndContrast(int brightness,double contrast, byte[] pixels) |
101 |
{ |
102 |
if (contrast < -100) contrast = -100; |
103 |
if (contrast > 100) contrast = 100; |
104 |
contrast = (100.0 + contrast) / 100.0; |
105 |
contrast *= contrast; |
106 |
|
107 |
for (int i = 0; i < pixels.Length / 4; ++i) |
108 |
{ |
109 |
byte b = pixels[i * 4]; |
110 |
byte g = pixels[i * 4 + 1]; |
111 |
byte r = pixels[i * 4 + 2]; |
112 |
byte a = pixels[i * 4 + 3]; |
113 |
|
114 |
if (r != 255 || g != 255 || b != 255 || a == 255) |
115 |
{ |
116 |
b = (byte)(b + brightness); |
117 |
g = (byte)(g + brightness); |
118 |
r = (byte)(r + brightness); |
119 |
///a = (byte)(a + brightness); |
120 |
|
121 |
if (r < 0) r = 1; |
122 |
if (r > 255) r = 255; |
123 |
|
124 |
if (g < 0) g = 1; |
125 |
if (g > 255) g = 255; |
126 |
|
127 |
if (b < 0) b = 1; |
128 |
if (b > 255) b = 255; |
129 |
|
130 |
double pR = r / 255.0; |
131 |
pR -= 0.5; |
132 |
pR *= contrast; |
133 |
pR += 0.5; |
134 |
pR *= 255; |
135 |
if (pR < 0) pR = 0; |
136 |
if (pR > 255) pR = 255; |
137 |
|
138 |
double pG = g / 255.0; |
139 |
pG -= 0.5; |
140 |
pG *= contrast; |
141 |
pG += 0.5; |
142 |
pG *= 255; |
143 |
if (pG < 0) pG = 0; |
144 |
if (pG > 255) pG = 255; |
145 |
|
146 |
double pB = b / 255.0; |
147 |
pB -= 0.5; |
148 |
pB *= contrast; |
149 |
pB += 0.5; |
150 |
pB *= 255; |
151 |
if (pB < 0) pB = 0; |
152 |
if (pB > 255) pB = 255; |
153 |
|
154 |
pixels[i * 4] = (byte)pB; |
155 |
pixels[i * 4 + 1] = (byte)pG; |
156 |
pixels[i * 4 + 2] = (byte)pR; |
157 |
} |
158 |
} |
159 |
|
160 |
return pixels; |
161 |
} |
162 |
|
163 |
public static byte[] SetContrast(double contrast, byte[] pixels) |
164 |
{ |
165 |
if (contrast < -100) contrast = -100; |
166 |
if (contrast > 100) contrast = 100; |
167 |
contrast = (100.0 + contrast) / 100.0; |
168 |
contrast *= contrast; |
169 |
|
170 |
for (int i = 0; i < pixels.Length / 4; ++i) |
171 |
{ |
172 |
byte b = pixels[i * 4]; |
173 |
byte g = pixels[i * 4 + 1]; |
174 |
byte r = pixels[i * 4 + 2]; |
175 |
byte a = pixels[i * 4 + 3]; |
176 |
|
177 |
if (r != 255 || g != 255 || b != 255 || a == 255) |
178 |
{ |
179 |
double pR = r / 255.0; |
180 |
pR -= 0.5; |
181 |
pR *= contrast; |
182 |
pR += 0.5; |
183 |
pR *= 255; |
184 |
if (pR < 0) pR = 0; |
185 |
if (pR > 255) pR = 255; |
186 |
|
187 |
double pG = g / 255.0; |
188 |
pG -= 0.5; |
189 |
pG *= contrast; |
190 |
pG += 0.5; |
191 |
pG *= 255; |
192 |
if (pG < 0) pG = 0; |
193 |
if (pG > 255) pG = 255; |
194 |
|
195 |
double pB = b / 255.0; |
196 |
pB -= 0.5; |
197 |
pB *= contrast; |
198 |
pB += 0.5; |
199 |
pB *= 255; |
200 |
if (pB < 0) pB = 0; |
201 |
if (pB > 255) pB = 255; |
202 |
|
203 |
pixels[i * 4] = (byte)pB; |
204 |
pixels[i * 4 + 1] = (byte)pG; |
205 |
pixels[i * 4 + 2] = (byte)pR; |
206 |
} |
207 |
} |
208 |
|
209 |
return pixels; |
210 |
} |
211 |
} |
212 |
} |