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
| using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;// 使用Marshal類別來配置Unmanaged記憶體。
using System.Threading.Tasks;// TPL
//處理邏輯:
public Bitmap Contraster(Bitmap bmp, Int32 level)
{
Double dlevel = Math.Sqrt((level % 10) + 1);
Bitmap newbmp = new Bitmap(bmp.Width, bmp.Height);
// orignal bitmap only for reading value, the clone one for writing new value.
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly, bmp.PixelFormat);
BitmapData newData = newbmp.LockBits(new Rectangle(0, 0, newbmp.Width, newbmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
Int32 Width = bmp.Width;
Int32 Height = bmp.Height;
Parallel.For(0, Height, delegate(Int32 height) {
Parallel.For(0, Width, delegate(Int32 width) {
Int32 Offset = height * bmpData.Stride + width * (bmpData.Stride / bmpData.Width);
Double Red = Marshal.ReadByte(bmpData.Scan0, Offset + 2);
Double Green = Marshal.ReadByte(bmpData.Scan0, Offset + 1);
Double Blue = Marshal.ReadByte(bmpData.Scan0, Offset);
Red = ((Red / 255.0 - 0.5) * dlevel + 0.5) * 255;
Red = Red > 255 ? 255 : Red;
Red = Red < 0 ? 0 : Red;
Green = ((Green / 255.0 - 0.5) * dlevel + 0.5) * 255;
Green = Green > 255 ? 255 : Green;
Green = Green < 0 ? 0 : Green;
Blue = ((Blue / 255.0 - 0.5) * dlevel + 0.5) * 255;
Blue = Blue > 255 ? 255 : Blue;
Blue = Blue < 0 ? 0 : Blue;
Marshal.WriteByte(newData.Scan0, Offset + 2, (Byte)Red);
Marshal.WriteByte(newData.Scan0, Offset + 1, (Byte)Green);
Marshal.WriteByte(newData.Scan0, Offset, (Byte)Blue);
});
});
bmp.UnlockBits(bmpData);
newbmp.UnlockBits(newData);
return newbmp;
}
|