c#将图片转为灰度图片
2018-07-20 来源:open-open
public static Image ConvertToGrayScale(Image orginalImage) { Bitmap newImage = new Bitmap(orginalImage); BitmapData orginalData = (orginalImage as Bitmap).LockBits(new Rectangle(0, 0, orginalImage.Width, orginalImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData newData = (newImage as Bitmap).LockBits(new Rectangle(0, 0, orginalImage.Width, orginalImage.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int orginalstride = orginalData.Stride; System.IntPtr orginalScan0 = orginalData.Scan0; int newstride = newData.Stride; System.IntPtr newScan0 = newData.Scan0; unsafe { byte* pOrginal = (byte*)(void*)orginalScan0; byte* pNew = (byte*)(void*)newScan0; int nOffset = orginalstride - orginalImage.Width * 3; byte red, green, blue; for (int y = 0; y < orginalImage.Height; ++y) { for (int x = 0; x < orginalImage.Width; ++x) { blue = pOrginal[0]; green = pOrginal[1]; red = pOrginal[2]; byte newPixel = Convert.ToByte((blue + red + green) / 3); pNew[0] = newPixel; pNew[1] = newPixel; pNew[2] = newPixel; pOrginal += 3; pNew += 3; } pOrginal += nOffset; pNew += nOffset; } } (orginalImage as Bitmap).UnlockBits(orginalData); (newImage as Bitmap).UnlockBits(newData); return newImage; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:python编写简单抽奖系统
下一篇: js使用setInterval实现倒计时
最新资讯
热门推荐