프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Common / ImageSourceHelper.cs @ df2e7646

이력 | 보기 | 이력해설 | 다운로드 (11 KB)

1 cdfb57ff taeseongkim
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 92442e4a taeseongkim
        public static async System.Threading.Tasks.Task<BitmapImage> GetDownloadImageAsync(Uri uri, double pageWidth, double pageHeight)
14 cdfb57ff taeseongkim
        {
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 566f0526 taeseongkim
58 0f6604ce taeseongkim
        public static BitmapImage GetDownloadImage(string uri)
59
        {
60
           return GetDownloadImage(new Uri(uri));
61
        }
62
63
        public static BitmapImage GetDownloadImage(Uri uri)
64
        {
65
            BitmapImage resultImage = new BitmapImage();
66
67
            resultImage.BeginInit();
68
            resultImage.CacheOption = BitmapCacheOption.OnLoad;
69
            resultImage.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Default);
70
            resultImage.UriSource = uri;
71
            resultImage.EndInit();
72
73
            resultImage.DownloadProgress += (ex, arg) =>
74
            {
75
                System.Diagnostics.Debug.WriteLine($"download Image : {arg.Progress}%");
76
            };
77
78
            while (resultImage.IsDownloading)
79
            {
80
                System.Windows.Forms.Application.DoEvents(); //Method from thread linked above
81
                System.Diagnostics.Debug.WriteLine("doEvent");
82
            }
83
84
            resultImage.Freeze();
85
            return resultImage;
86
        }
87
88 566f0526 taeseongkim
        public static byte[] CopyPixels(BitmapSource image)
89
        {
90
            byte[] result = new byte[image.PixelWidth * image.PixelHeight * 4];
91
            image.CopyPixels(result, image.PixelWidth * 4, 0);
92
93
            return result;
94
        }
95
96
        public static byte[] SetBrightness(int brightness, byte[] pixels)
97
        {
98
            for (int i = 0; i < pixels.Length / 4; ++i)
99
            {
100
                byte b = pixels[i * 4];
101
                byte g = pixels[i * 4 + 1];
102
                byte r = pixels[i * 4 + 2];
103
                byte a = pixels[i * 4 + 3];
104
105
                if (r != 255 || g != 255 || b != 255 || a == 255)
106
                {
107
                    b = (byte)(b + brightness);
108
                    g = (byte)(g + brightness);
109
                    r = (byte)(r + brightness);
110
                    ///a = (byte)(a + brightness);
111
112
                    if (r < 0) r = 1;
113
                    if (r > 255) r = 255;
114
115
                    if (g < 0) g = 1;
116
                    if (g > 255) g = 255;
117
118
                    if (b < 0) b = 1;
119
                    if (b > 255) b = 255;
120
121
                    pixels[i * 4] = b;
122
                    pixels[i * 4 + 1] = g;
123
                    pixels[i * 4 + 2] = r;
124
                }
125
            }
126
127
            return pixels;
128
        }
129
130
        public static byte[] SetBrightnessAndContrast(int brightness,double contrast, byte[] pixels)
131
        {
132 997071b8 taeseongkim
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
133
            stopwatch.Start();
134
135 566f0526 taeseongkim
            if (contrast < -100) contrast = -100;
136
            if (contrast > 100) contrast = 100;
137
            contrast = (100.0 + contrast) / 100.0;
138
            contrast *= contrast;
139
140
            for (int i = 0; i < pixels.Length / 4; ++i)
141
            {
142
                byte b = pixels[i * 4];
143
                byte g = pixels[i * 4 + 1];
144
                byte r = pixels[i * 4 + 2];
145
                byte a = pixels[i * 4 + 3];
146
147 997071b8 taeseongkim
                //System.Diagnostics.Debug.WriteLineIf(i< 4, $"B :{b} G : {g} r : {r}");
148
149
                if (r != 255 || g != 255 || b != 255)
150 566f0526 taeseongkim
                {
151
                    b = (byte)(b + brightness);
152
                    g = (byte)(g + brightness);
153
                    r = (byte)(r + brightness);
154
                    ///a = (byte)(a + brightness);
155
156
                    if (r < 0) r = 1;
157
                    if (r > 255) r = 255;
158
159
                    if (g < 0) g = 1;
160
                    if (g > 255) g = 255;
161
162
                    if (b < 0) b = 1;
163
                    if (b > 255) b = 255;
164
165
                    double pR = r / 255.0;
166
                    pR -= 0.5;
167
                    pR *= contrast;
168
                    pR += 0.5;
169
                    pR *= 255;
170
                    if (pR < 0) pR = 0;
171
                    if (pR > 255) pR = 255;
172
173
                    double pG = g / 255.0;
174
                    pG -= 0.5;
175
                    pG *= contrast;
176
                    pG += 0.5;
177
                    pG *= 255;
178
                    if (pG < 0) pG = 0;
179
                    if (pG > 255) pG = 255;
180
181
                    double pB = b / 255.0;
182
                    pB -= 0.5;
183
                    pB *= contrast;
184
                    pB += 0.5;
185
                    pB *= 255;
186
                    if (pB < 0) pB = 0;
187
                    if (pB > 255) pB = 255;
188
189
                    pixels[i * 4] = (byte)pB;
190
                    pixels[i * 4 + 1] = (byte)pG;
191
                    pixels[i * 4 + 2] = (byte)pR;
192 fc9da8df taeseongkim
              }
193 566f0526 taeseongkim
            }
194
195 997071b8 taeseongkim
            System.Diagnostics.Debug.WriteLine($"일반 : {stopwatch.Elapsed.ToString()}");
196
197
            return pixels;
198
        }
199
200
        public static byte[] SetBrightnessAndContrastLinp(int brightness, double contrast, byte[] pixels)
201
        {
202
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
203
            stopwatch.Start();
204
205
            if (contrast < -100) contrast = -100;
206
207
            if (contrast > 100) contrast = 100;
208
209
            contrast = (100.0 + contrast) / 100.0;
210
211
            contrast *= contrast;
212
213
            Parallel.ForEach(SteppedIntegerList(pixels,0, pixels.Length, 4,4),new ParallelOptions {MaxDegreeOfParallelism = 5 }, item=>
214
            {
215
                var pixel = item.ToList();
216
217
                byte b = pixel[0].Value;
218
                byte g = pixel[1].Value;
219
                byte r = pixel[2].Value;
220
                byte a = pixel[3].Value;
221
222
                //System.Diagnostics.Debug.WriteLineIf(pixel[0].Key < 4, $"B :{b} G : {g} r : {r}");
223
224
                if (r != 255 || g != 255 || b != 255)
225
                {
226
                    b = (byte)(b + brightness);
227
                    g = (byte)(g + brightness);
228
                    r = (byte)(r + brightness);
229
                    ///a = (byte)(a + brightness);
230
231
                    if (r < 0) r = 1;
232
                    if (r > 255) r = 255;
233
234
                    if (g < 0) g = 1;
235
                    if (g > 255) g = 255;
236
237
                    if (b < 0) b = 1;
238
                    if (b > 255) b = 255;
239
240
                    double pR = r / 255.0;
241
                    pR -= 0.5;
242
                    pR *= contrast;
243
                    pR += 0.5;
244
                    pR *= 255;
245
                    if (pR < 0) pR = 0;
246
                    if (pR > 255) pR = 255;
247
248
                    double pG = g / 255.0;
249
                    pG -= 0.5;
250
                    pG *= contrast;
251
                    pG += 0.5;
252
                    pG *= 255;
253
                    if (pG < 0) pG = 0;
254
                    if (pG > 255) pG = 255;
255
256
                    double pB = b / 255.0;
257
                    pB -= 0.5;
258
                    pB *= contrast;
259
                    pB += 0.5;
260
                    pB *= 255;
261
                    if (pB < 0) pB = 0;
262
                    if (pB > 255) pB = 255;
263
264
                    pixels[pixel[0].Key] = (byte)pB;
265
                    pixels[pixel[1].Key] = (byte)pG;
266
                    pixels[pixel[2].Key] = (byte)pR;
267
                }
268
            });
269
270
            System.Diagnostics.Debug.WriteLine($"Linq : {stopwatch.Elapsed.ToString()}");
271
272 566f0526 taeseongkim
            return pixels;
273
        }
274
275 997071b8 taeseongkim
        public static IEnumerable<Dictionary<int,T>> SteppedIntegerList<T>(T[] array, int startIndex,int endEndex, int stepSize,int TakeSize)
276
        {
277
            for (int i = startIndex; i < endEndex; i += stepSize)
278
            {
279
                Dictionary<int, T> result = new Dictionary<int, T>();
280
281
                for (int j = 0; j < TakeSize; j++)
282
                {
283
                    result.Add(j, array[i + j]);
284
                }
285
                
286
                yield return result;
287
            }
288
        }
289
290 566f0526 taeseongkim
        public static byte[] SetContrast(double contrast, byte[] pixels)
291
        {
292
            if (contrast < -100) contrast = -100;
293
            if (contrast > 100) contrast = 100;
294
            contrast = (100.0 + contrast) / 100.0;
295
            contrast *= contrast;
296
297
            for (int i = 0; i < pixels.Length / 4; ++i)
298
            {
299
                byte b = pixels[i * 4];
300
                byte g = pixels[i * 4 + 1];
301
                byte r = pixels[i * 4 + 2];
302
                byte a = pixels[i * 4 + 3];
303
304
                if (r != 255 || g != 255 || b != 255 || a == 255)
305
                {
306
                    double pR = r / 255.0;
307
                    pR -= 0.5;
308
                    pR *= contrast;
309
                    pR += 0.5;
310
                    pR *= 255;
311
                    if (pR < 0) pR = 0;
312
                    if (pR > 255) pR = 255;
313
314
                    double pG = g / 255.0;
315
                    pG -= 0.5;
316
                    pG *= contrast;
317
                    pG += 0.5;
318
                    pG *= 255;
319
                    if (pG < 0) pG = 0;
320
                    if (pG > 255) pG = 255;
321
322
                    double pB = b / 255.0;
323
                    pB -= 0.5;
324
                    pB *= contrast;
325
                    pB += 0.5;
326
                    pB *= 255;
327
                    if (pB < 0) pB = 0;
328
                    if (pB > 255) pB = 255;
329
330
                    pixels[i * 4] = (byte)pB;
331
                    pixels[i * 4 + 1] = (byte)pG;
332
                    pixels[i * 4 + 2] = (byte)pR;
333
                }
334
            }
335
336
            return pixels;
337
        }
338 cdfb57ff taeseongkim
    }
339
}
클립보드 이미지 추가 (최대 크기: 500 MB)