码迷,mamicode.com
首页 > 其他好文 > 详细

简单图片处理

时间:2014-06-13 08:05:42      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   ext   color   com   

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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
public static class ImageProcess
{
    /// <summary>
    /// 图片旋转
    /// </summary>
    /// <param name="image"></param>
    /// <param name="angle">旋转角度</param>
    /// <returns></returns>
    public static Image Rotation(Image image,float angle)
    {
 
        angle = 360 - angle % 360;            //弧度转换
        double radian = angle * Math.PI / 180.0;
        double cos = Math.Cos(radian);
        double sin = Math.Sin(radian);
        //原图的宽和高
        int w = image.Width;
        int h = image.Height;
        int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
        int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
        //目标位图
        Bitmap dsImage = new Bitmap(W, H);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //计算偏移量
        Point Offset = new Point((W - w) / 2, (H - h) / 2);
        //构造图像显示区域:让图像的中心与窗口的中心点一致
        Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
        Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
        g.TranslateTransform(center.X, center.Y);
        g.RotateTransform(360 - angle);
        //恢复图像在水平和垂直方向的平移
        g.TranslateTransform(-center.X, -center.Y);
        g.DrawImage(image, rect);
        //重至绘图的所有变换
        g.ResetTransform();
        g.Save();
        g.Dispose();
        return dsImage;
    }
 
    /// <summary>
    /// 图片翻转
    /// </summary>
    /// <param name="image"></param>
    /// <param name="type">1水平,2垂直,3水平垂直</param>
    /// <returns></returns>
    public static Image Turn(Image image,int type)
    {
        if (type == 1)
        {
            image.RotateFlip(RotateFlipType.RotateNoneFlipX);
        }
        else if (type == 2)
        {
            image.RotateFlip(RotateFlipType.RotateNoneFlipY);
        }
        else if (type == 3)
        {
            image.RotateFlip(RotateFlipType.RotateNoneFlipXY);
        }
        return image;
    }
 
    /// <summary>
    /// 底片效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Film(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel;
            for (int x = 1; x < width; x++)
            {
                for (int y = 1; y < height; y++)
                {
                    int r, g, b;
                    pixel = oldbitmap.GetPixel(x, y);
                    r = 255 - pixel.R;
                    g = 255 - pixel.G;
                    b = 255 - pixel.B;
                    newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 浮雕效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Relief(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel1, pixel2;
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    int r, g, b;
                    pixel1 = oldbitmap.GetPixel(x, y);
                    pixel2 = oldbitmap.GetPixel(x + 1, y + 1);
                    r = Math.Abs(pixel1.R - pixel2.R + 128);
                    g = Math.Abs(pixel1.G - pixel2.G + 128);
                    b = Math.Abs(pixel1.B - pixel2.B + 128);
                    if (r > 255)
                        r = 255;
                    if (r < 0)
                        r = 0;
                    if (g > 255)
                        g = 255;
                    if (g < 0)
                        g = 0;
                    if (b > 255)
                        b = 255;
                    if (b < 0)
                        b = 0;
                    newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 黑白效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image BlackAndWhite(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel;
            for (int x = 0; x < width; x++)
                for (int y = 0; y < height; y++)
                {
                    pixel = oldbitmap.GetPixel(x, y);
                    int r, g, b, Result = 0;
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    //实例程序以加权平均值法产生黑白图像
                    int iType = 2;
                    switch (iType)
                    {
                        case 0://平均值法
                            Result = ((r + g + b) / 3);
                            break;
                        case 1://最大值法
                            Result = r > g ? r : g;
                            Result = Result > b ? Result : b;
                            break;
                        case 2://加权平均值法
                            Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                            break;
                    }
                    newbitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 柔化效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Soften(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel;
            //高斯模板
            int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = oldbitmap.GetPixel(x + row, y + col);
                            r += pixel.R * Gauss[Index];
                            g += pixel.G * Gauss[Index];
                            b += pixel.B * Gauss[Index];
                            Index++;
                        }
                    r /= 16;
                    g /= 16;
                    b /= 16;
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newbitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 锐化效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Sharpen(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel;
            //拉普拉斯模板
            int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    int r = 0, g = 0, b = 0;
                    int Index = 0;
                    for (int col = -1; col <= 1; col++)
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = oldbitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                            g += pixel.G * Laplacian[Index];
                            b += pixel.B * Laplacian[Index];
                            Index++;
                        }
                    //处理颜色值溢出
                    r = r > 255 ? 255 : r;
                    r = r < 0 ? 0 : r;
                    g = g > 255 ? 255 : g;
                    g = g < 0 ? 0 : g;
                    b = b > 255 ? 255 : b;
                    b = b < 0 ? 0 : b;
                    newbitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                }
            }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 雾化效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Atomization(Image image)
    {
        try
        {
            int height = image.Height;
            int width = image.Width;
            Bitmap newbitmap = new Bitmap(width, height);
            Bitmap oldbitmap = (Bitmap)image;
            Color pixel;
            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    System.Random MyRandom = new Random();
                    int k = MyRandom.Next(123456);
                    //像素块大小
                    int dx = x + k % 9;
                    int dy = y + k % 9;
                    if (dx >= width)
                        dx = width - 1;
                    if (dy >= height)
                        dy = height - 1;
                    pixel = oldbitmap.GetPixel(dx, dy);
                    newbitmap.SetPixel(x, y, pixel);
                }
            }
            return newbitmap;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 图片明暗调整
    /// </summary>
    /// <param name="image"></param>
    /// <param name="degree">亮度[-255,255]</param>
    /// <returns></returns>
    public static Image Brightness(Image image, int degree)
    {
        try
        {
            Bitmap b = (Bitmap)image;
            if (degree < -255) degree = -255;
            if (degree > 255) degree = 255;
            int width = image.Width;
            int height = image.Height;
            int pix = 0;
 
            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
 
            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        // 处理指定位置像素的亮度
                        for (int i = 0; i < 3; i++)
                        {
                            pix = p[i] + degree;
 
                            if (degree < 0) p[i] = (byte)Math.Max(0, pix);
                            if (degree > 0) p[i] = (byte)Math.Min(255, pix);
 
                        } // i
                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
 
            b.UnlockBits(data);
 
            return image;
        }
        catch
        {
            return null;
        }
    }
 
    /// <summary>
    /// 灰色效果
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image Gray(Image image)
    {
        try
        {
            Bitmap currentBitmap = (Bitmap)image.Clone();
            Graphics g = Graphics.FromImage(currentBitmap);
            ImageAttributes ia = new ImageAttributes();
            float[][] colorMatrix =   {   
            new   float[]   {0.299f,   0.299f,   0.299f,   0,   0},
            new   float[]   {0.587f,   0.587f,   0.587f,   0,   0},
            new   float[]   {0.114f,   0.114f,   0.114f,   0,   0},
            new   float[]   {0,   0,   0,   1,   0},
            new   float[]   {0,   0,   0,   0,   1}
                                      };
            ColorMatrix cm = new ColorMatrix(colorMatrix);
            ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            g.DrawImage(currentBitmap, new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height), 0, 0, currentBitmap.Width, currentBitmap.Height, GraphicsUnit.Pixel, ia);
            g.Dispose();
            return (Image)(currentBitmap.Clone());
        }
        catch
        {
            return null;
        }
         
    }
 
    /// <summary>
    /// 图像对比度调整
    /// </summary>
    /// <param name="b">原始图</param>
    /// <param name="degree">对比度[-100, 100]</param>
    /// <returns></returns>
    public static Bitmap KiContrast(Bitmap b, int degree)
    {
        if (b == null)
        {
            return null;
        }
 
        if (degree < -100) degree = -100;
        if (degree > 100) degree = 100;
 
        try
        {
 
            double pixel = 0;
            double contrast = (100.0 + degree) / 100.0;
            contrast *= contrast;
            int width = b.Width;
            int height = b.Height;
            BitmapData data = b.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                byte* p = (byte*)data.Scan0;
                int offset = data.Stride - width * 3;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        // 处理指定位置像素的对比度
                        for (int i = 0; i < 3; i++)
                        {
                            pixel = ((p[i] / 255.0 - 0.5) * contrast + 0.5) * 255;
                            if (pixel < 0) pixel = 0;
                            if (pixel > 255) pixel = 255;
                            p[i] = (byte)pixel;
                        } // i
                        p += 3;
                    } // x
                    p += offset;
                } // y
            }
            b.UnlockBits(data);
            return b;
        }
        catch
        {
            return null;
        }
    }
}

  

简单图片处理,布布扣,bubuko.com

简单图片处理

标签:class   blog   code   ext   color   com   

原文地址:http://www.cnblogs.com/fuchao2014/p/3781204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!