虽然现在在图像图形上想提速,公认还是使用GPU了。但是如果机器没有一个好的显卡?你只是想写一个简单的.Net程序?GDI在其平台的易用性还是有很强的市场。只是一旦你需要使用GDI多次甚至大量的绘制图像时候,当你只是想简单的但是多次的复制图片的时候,使用Graphics.DrawImage()的性能绝对会崩溃。
查了很久,Gameres几个哥们给了几个很好的建议,主要思路是直接对Bitmap位操纵,这样子还可以利用多线程(注意GDI是不能多线程调用的,至少我用TPL库就不成功)。
下面用几个具体函数说明。
第一个是从一个Source Bitmap的Rectangle sourceRect的地址的内容拷贝到 Dest Bitmap 的 Point destPos起点的地址。
//Copy the data of sourceMap in sourceRect to destMap start from destPos
//Currently the method only deals with the bitmap with format Format32bppArgb.
public bool CopyBitMapWithLockBits(Bitmap sourceMap, ref Bitmap destMap, Rectangle sourceRect, Point destPos)
{
if (destPos.X + sourceRect.Width > destMap.Width ||
destPos.Y + sourceRect.Height > destMap.Height)
return false;
//Key Step1 :Use LockBits() to lock the address of bitmap you want to write in.
BitmapData sourceBitmapData = sourceMap.LockBits(new Rectangle(sourceRect.X, sourceRect.Y, sourceRect.Width, sourceRect.Height), ImageLockMode.ReadOnly, sourceMap.PixelFormat);
BitmapData destBitmapData = destMap.LockBits(new Rectangle(destPos.X, destPos.Y, sourceRect.Width, sourceRect.Height), ImageLockMode.WriteOnly, sourceMap.PixelFormat);
//Create the temp lock memory for transferring
int ibytes = sourceRect.Width * 4 * sizeof(byte);
byte[] rgbValues = new byte[ibytes];
//Key Step2 : Copy the data from sourceMap to destMap . Notice you can't copy from a bitmap directly to another bitmap.You must use an intermediate adrress.
for (int i = 0; i < sourceRect.Height; i++)
{
IntPtr sourceAddress = (IntPtr)sourceBitmapData.Scan0.ToInt64() + i * sourceBitmapData.Stride;
IntPtr destAddress = (IntPtr)(destBitmapData.Scan0.ToInt64() + i * destBitmapData.Stride);
Marshal.Copy(sourceAddress, rgbValues, 0, sourceRect.Width * 4);
Marshal.Copy(rgbValues, 0,
destAddress, sourceRect.Width * 4);
}
//Key Step3 : Unlock the address
sourceMap.UnlockBits(sourceBitmapData);
destMap.UnlockBits(destBitmapData);
return true;
}
第二个Demo是改写Graphics.FillRectangle(),直接自己对这片矩形地址填充ARGB的值。
//Fill the bitmap in rect with color.This method is used to replace GDI.
public void FillBitmap(ref Bitmap bitmap, Rectangle rect, Color color)
{
//=====Lock the whole region with the lockbits
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
//get the number of lock memory
int ibytes = rect.Width * rect.Height * 4 * sizeof(byte);
//The address for intermediate computing and storing.
byte[] rgbValues = new byte[ibytes];
//initialize the first line
for (int j = 0; j < rect.Width; j++)
{
rgbValues[4 * j + 0] = color.B;
rgbValues[4 * j + 1] = color.G;
rgbValues[4 * j + 2] = color.R;
rgbValues[4 * j + 3] = color.A;
}
//initialize the rest lines of rgvValues with copy
for (int i = 1; i < rect.Height; i++)
{
Buffer.BlockCopy(rgbValues, 0, rgbValues, i * rect.Width * 4, rect.Width * 4);
}
//copy the data from intermediate address to bitmap one line after another
for (int i = 0; i < rect.Height; i++)
{
IntPtr destAddress = (IntPtr)(bitmapData.Scan0.ToInt64() + rect.Y * bitmapData.Stride + rect.X * 4 * sizeof(byte) + i * bitmapData.Stride);
Marshal.Copy(rgbValues, i * rect.Width * 4,
destAddress, rect.Width * 4);
}
bitmap.UnlockBits(bitmapData);
}
因为我需要大量的调用DrawImage() 和FillRectangle(),经过这样一改写后,速度提升了200%有多。。