快速將彩色照片變成黑白照片(僅適用于jpg格式)
上篇文章寫了將彩色圖片變成黑白照片的一個方法,該方法遍歷圖片色素,然后通過公式將彩色變成黑白,效率十分低下。
今天做了下修改,效率大概提高了1000倍吧
測試圖如下:

214452542是上個方法的執(zhí)行時間
250169是本次方法執(zhí)行時間
圖如本次方法生成圖效果
更改了公式為:Y=0.299*R+0.114*G+0.587B
代碼:
特別感謝:YaoTong
ahnan
沐楓
三位兄弟在上篇文章對我的幫助!
今天做了下修改,效率大概提高了1000倍吧
測試圖如下:
214452542是上個方法的執(zhí)行時間
250169是本次方法執(zhí)行時間
圖如本次方法生成圖效果
更改了公式為:Y=0.299*R+0.114*G+0.587B
代碼:
1
/// <summary>
2
/// 快速的將彩色圖像變成黑白圖像-目前僅適用于jpg格式的圖像
3
/// </summary>
4
/// <param name="filePath">彩色圖像地址</param>
5
/// <returns>返回的黑白圖像</returns>
6
public static Bitmap QuickWhiteAndBlack(string filePath)
7
{
8
9
// 從文件創(chuàng)建Bitmap對象
10
Bitmap bmp = new Bitmap(filePath);
11
12
// 將Bitmap鎖定到系統(tǒng)內(nèi)存中
13
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
14
// 獲得BitmapData
15
System.Drawing.Imaging.BitmapData bmpData =
16
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
17
bmp.PixelFormat);
18
19
// 位圖中第一個像素數(shù)據(jù)的地址。它也可以看成是位圖中的第一個掃描行
20
IntPtr ptr = bmpData.Scan0;
21
22
// 將Bitmap對象的信息存放到byte數(shù)組中
23
// 假設(shè)位圖中一個像素包含3byte,也就是24bit
24
int bytes = bmp.Width * bmp.Height * 3;
25
byte[] rgbValues = new byte[bytes];
26
27
//復制GRB信息到byte數(shù)組
28
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
29
30
// 根據(jù)Y=0.299*R+0.114*G+0.587B,Y為亮度
31
for (int counter = 0; counter < rgbValues.Length; counter += 3)
32
{
33
byte value = (byte)(rgbValues[counter] * 0.299 + rgbValues[counter + 2] * 0.114 + rgbValues[counter + 1] * 0.587);
34
rgbValues[counter] = value;
35
rgbValues[counter + 1] = value;
36
rgbValues[counter + 2] = value;
37
}
38
39
//將更改過的byte[]拷貝到原位圖
40
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
41
42
// 解鎖位圖
43
bmp.UnlockBits(bmpData);
44
return bmp;
45
46
}
上篇地址:http://www.rzrgm.cn/jillzhang/archive/2006/10/09/524571.html
/// <summary>2
/// 快速的將彩色圖像變成黑白圖像-目前僅適用于jpg格式的圖像 3
/// </summary>4
/// <param name="filePath">彩色圖像地址</param>5
/// <returns>返回的黑白圖像</returns>6
public static Bitmap QuickWhiteAndBlack(string filePath)7
{8

9
// 從文件創(chuàng)建Bitmap對象10
Bitmap bmp = new Bitmap(filePath);11

12
// 將Bitmap鎖定到系統(tǒng)內(nèi)存中13
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);14
// 獲得BitmapData15
System.Drawing.Imaging.BitmapData bmpData =16
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,17
bmp.PixelFormat);18

19
// 位圖中第一個像素數(shù)據(jù)的地址。它也可以看成是位圖中的第一個掃描行20
IntPtr ptr = bmpData.Scan0;21

22
// 將Bitmap對象的信息存放到byte數(shù)組中23
// 假設(shè)位圖中一個像素包含3byte,也就是24bit24
int bytes = bmp.Width * bmp.Height * 3;25
byte[] rgbValues = new byte[bytes];26

27
//復制GRB信息到byte數(shù)組28
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);29

30
// 根據(jù)Y=0.299*R+0.114*G+0.587B,Y為亮度31
for (int counter = 0; counter < rgbValues.Length; counter += 3)32
{33
byte value = (byte)(rgbValues[counter] * 0.299 + rgbValues[counter + 2] * 0.114 + rgbValues[counter + 1] * 0.587);34
rgbValues[counter] = value;35
rgbValues[counter + 1] = value;36
rgbValues[counter + 2] = value;37
}38

39
//將更改過的byte[]拷貝到原位圖40
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);41

42
// 解鎖位圖43
bmp.UnlockBits(bmpData);44
return bmp;45

46
}特別感謝:YaoTong
ahnan
沐楓
三位兄弟在上篇文章對我的幫助!
作者:jillzhang
出處:http://jillzhang.cnblogs.com/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權(quán)利。
出處:http://jillzhang.cnblogs.com/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權(quán)利。



浙公網(wǎng)安備 33010602011771號