public void mbPaintBitUpdated(mbWebView webview, IntPtr param, IntPtr buffer, ref mbRect r, int width, int height) { Bitmap image; if (_image == null || _image.Width < width || _image.Height < height) { lock (this) { _image = new Bitmap(width, height, PixelFormat.Format32bppArgb); _drawSize = new Size(width, height); image = _image; } } else { image = _image; } unsafe { //计算更新区域 var rect = new Rectangle { X = r.x >= width ? width - 1 : r.x, Y = r.y >= height ? height - 1 : r.y }; rect.Width = rect.X + r.w > width ? width - rect.X : r.w; rect.Height = rect.Y + r.h > height ? height - rect.Y : r.h; BitmapData bitmapData = image.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); IntPtr ptr = bitmapData.Scan0; int targetStride = image.Width; int sourceStride = width; int *target = (int *)ptr; int *source = (int *)buffer; int targetPix; int sourcePix; for (int h = 0; h < rect.Height; h++) { for (int w = 0; w < rect.Width; w++) { targetPix = targetStride * h + w; sourcePix = sourceStride * (rect.Y + h) + (rect.X + w); *(target + targetPix) = *(source + sourcePix); } } _image.UnlockBits(bitmapData); Invalidate(rect); } }
public void mbPaintBitUpdated(mbWebView webview, IntPtr param, IntPtr buffer, ref mbRect r, int width, int height) { int stride = width * 4; if (_image == null || _image.PixelWidth < width || _image.PixelHeight < height) { _image = new WriteableBitmap(width, height, 96, 96, PixelFormats.Pbgra32, BitmapPalettes.WebPaletteTransparent); InvalidateVisual(); } //计算更新区域 var rect = new Int32Rect(); rect.X = r.x >= width ? width - 1 : r.x; rect.Y = r.y >= height ? height - 1 : r.y; rect.Width = rect.X + r.w > width ? width - rect.X : r.w; rect.Height = rect.Y + r.h > height ? height - rect.Y : r.h; _image.WritePixels(rect, buffer, stride * (rect.Height + rect.Y), stride, r.x, r.y); _drawSize = new Size(width, height); }