protected override void OnSizeChanged(EventArgs e) { VirtualDevice device = Device; if (device != null) { float xScale = 1; float yScale = 1; float ppd = OffscreenGraphics.PixelsPerDip; CefRect viewportRect = device.ViewportRect; viewportRect.Scale(OffscreenGraphics.PixelsPerDip); Rectangle clientRect = this.ClientRectangle; if (viewportRect.Height > clientRect.Height) { yScale = Math.Max(clientRect.Height - 6.0f, 1.0f) / viewportRect.Height; } if (viewportRect.Width > clientRect.Width) { xScale = Math.Max(clientRect.Width - 6.0f, 1.0f) / viewportRect.Width; } device.Scale = Math.Min(xScale, yScale); CefRect bounds = device.GetBounds(ppd); device.X = (int)(((clientRect.Width - bounds.Width) >> 1) / ppd); device.Y = (int)(((clientRect.Height - bounds.Height) >> 1) / ppd); CefInvalidate(); } base.OnSizeChanged(e); }
protected CefRect GetViewportRect() { CefRect viewportRect; VirtualDevice device = Device; if (device == null) { viewportRect = new CefRect(0, 0, this.Width, this.Height); viewportRect.Scale(OffscreenGraphics.PixelsPerDip); return(viewportRect); } else { return(device.GetBounds(OffscreenGraphics.PixelsPerDip)); } }
public unsafe CefRect Draw(CefPaintEventArgs e) { float ppd = OffscreenGraphics.PixelsPerDip; VirtualDevice device = this.Device; CefRect[] dirtyRects = e.DirtyRects; if (dirtyRects.Length == 0) { return(new CefRect()); } CefRect r = dirtyRects[0]; CefRect invalidRect = new CefRect(r.X, r.Y, r.Width, r.Height); for (int i = 1; i < dirtyRects.Length; i++) { invalidRect.Union(dirtyRects[i]); } if (device != null) { invalidRect.Scale(device.Scale * ppd / device.DevicePixelRatio); } if (e.PaintElementType == CefPaintElementType.Popup) { invalidRect.Offset(_popupBounds.X, _popupBounds.Y); } if (invalidRect.IsNullSize) { return(new CefRect()); } lock (_syncRoot) { int width = e.Width; int height = e.Height; if (device != null) { if (e.PaintElementType == CefPaintElementType.View) { width = (int)(_bounds.Width * device.Scale * ppd); height = (int)(_bounds.Height * device.Scale * ppd); } else if (e.PaintElementType == CefPaintElementType.Popup) { width = (int)(e.Width / device.DevicePixelRatio * device.Scale * ppd); height = (int)(e.Height / device.DevicePixelRatio * device.Scale * ppd); } } PixelBuffer pixelBuffer; if (e.PaintElementType == CefPaintElementType.View) { if (ViewPixels == null || ViewPixels.Width != width || ViewPixels.Height != height) { if (ViewPixels != null) { ViewPixels.Dispose(); } ViewPixels = new PixelBuffer(width, height); } pixelBuffer = ViewPixels; } else if (e.PaintElementType == CefPaintElementType.Popup) { if (PopupPixels == null || PopupPixels.Width != width || PopupPixels.Height != height) { if (PopupPixels != null) { PopupPixels.Dispose(); } PopupPixels = new PixelBuffer(width, height); } pixelBuffer = PopupPixels; } else { return(new CefRect()); } if (e.Width == pixelBuffer.Width && e.Height == pixelBuffer.Height) { long bufferSize = pixelBuffer.Source.ByteCount; Buffer.MemoryCopy(e.Buffer.ToPointer(), pixelBuffer.Source.GetPixels().ToPointer(), bufferSize, bufferSize); } else { using (var canvas = new SKCanvas(pixelBuffer.Source)) using (var paint = new SKPaint()) { canvas.Clear(); paint.FilterQuality = SKFilterQuality.Medium; using (var source = new SKBitmap()) { source.InstallPixels(new SKImageInfo(e.Width, e.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), e.Buffer); canvas.DrawBitmap(source, new SKRect(0, 0, pixelBuffer.Width, pixelBuffer.Height), paint); } canvas.Flush(); } } } invalidRect.Inflate(2, 2); return(invalidRect); }
public CefRect Draw(CefPaintEventArgs e) { float ppd = OffscreenGraphics.PixelsPerDip; VirtualDevice device = this.Device; CefRect[] dirtyRects = e.DirtyRects; if (dirtyRects.Length == 0) { return(new CefRect()); } CefRect r = dirtyRects[0]; CefRect invalidRect = new CefRect(r.X, r.Y, r.Width, r.Height); for (int i = 1; i < dirtyRects.Length; i++) { invalidRect.Union(dirtyRects[i]); } if (device != null) { invalidRect.Scale(device.Scale * ppd / device.DevicePixelRatio); } if (e.PaintElementType == CefPaintElementType.Popup) { invalidRect.Offset(_popupBounds.X, _popupBounds.Y); } if (invalidRect.IsNullSize) { return(new CefRect()); } lock (_syncRoot) { int width = e.Width; int height = e.Height; if (device != null) { if (e.PaintElementType == CefPaintElementType.View) { width = (int)(_bounds.Width * device.Scale * ppd); height = (int)(_bounds.Height * device.Scale * ppd); } else if (e.PaintElementType == CefPaintElementType.Popup) { width = (int)(e.Width / device.DevicePixelRatio * device.Scale * ppd); height = (int)(e.Height / device.DevicePixelRatio * device.Scale * ppd); } } PixelBuffer pixelBuffer; if (e.PaintElementType == CefPaintElementType.View) { if (ViewPixels == null || ViewPixels.Width != width || ViewPixels.Height != height) { if (ViewPixels != null) { ViewPixels.Dispose(); } ViewPixels = new PixelBuffer(width, height); } pixelBuffer = ViewPixels; } else if (e.PaintElementType == CefPaintElementType.Popup) { if (PopupPixels == null || PopupPixels.Width != width || PopupPixels.Height != height) { if (PopupPixels != null) { PopupPixels.Dispose(); } PopupPixels = new PixelBuffer(width, height); } pixelBuffer = PopupPixels; } else { return(new CefRect()); } using (Graphics g = Graphics.FromImage(pixelBuffer.Source)) { Color background = this.Background; if (background.A > 0) { using (var brush = new SolidBrush(background)) { g.FillRectangle(brush, 0, 0, width, height); } } using (var bitmap = new Bitmap(e.Width, e.Height, e.Width << 2, PixelFormat.Format32bppArgb, e.Buffer)) { if (e.Width != width || e.Height != height) { g.CompositingQuality = CompositingQuality.HighSpeed; g.InterpolationMode = this.InterpolationMode; g.DrawImage(bitmap, 0, 0, width, height); } else { g.DrawImage(bitmap, 0, 0); } g.Flush(); } } } invalidRect.Inflate(2, 2); return(invalidRect); }