有没有更好的办法,这个感觉时间太久了
wpf中的图片有没有好用的,这里用的是winform中的bitmap
C#可以用lockbits和unlockbits方法把bmp图像写进内存中,然后操作。这样效率很高。比如下面是把bmp图像转为灰度图像的代码。
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] bytes=new byte[bData.Stride*bData.Height];
System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, bytes, 0, bytes.Length);
for (int j = 0; j < bData.Height; j++)
for (int i = 0; i < bData.Width; i++)
{
int pixel = j * bData.Stride + 3 * i;
byte avg=(byte)((bytes[pixel]+bytes[pixel+1]+bytes[pixel+2])/3);
bytes[pixel] = bytes[pixel + 1] = bytes[pixel + 2] = avg;
}
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bData.Scan0, bytes.Length);
bmp.UnlockBits(bData);
首先在数组里写你的数据,然后
MemoryStream stream = null;
stream = new MemoryStream(byte数组);
bmp = new Bitmap((Image)new Bitmap(stream));
用逐行扫描方法scanline,再列循环读取,性能应该会有提升。