internal void Cleanup(ref WriteableBitmap backbuffer) { if (backbuffer == null || backbuffer.Width != this.size.Width || backbuffer.Height != this.size.Height) { backbuffer = new WriteableBitmap( this.size.Width, this.size.Height, 96, 96, PixelFormats.Pbgra32, BitmapPalettes.WebPalette); readBuf = new byte[this.size.Width * this.size.Height * 4]; } GL.ReadPixels(0, 0, this.size.Width, this.size.Height, PixelFormat.Bgra, PixelType.UnsignedByte, readBuf); // copy pixels upside down backbuffer.Lock(); var src = new Int32Rect(0, 0, (int)backbuffer.Width, 1); for (int y = 0; y < (int)backbuffer.Height; y++) { src.Y = (int)backbuffer.Height - y - 1; backbuffer.WritePixels(src, readBuf, backbuffer.BackBufferStride, 0, y); } backbuffer.AddDirtyRect(new Int32Rect(0, 0, (int)backbuffer.Width, (int)backbuffer.Height)); backbuffer.Unlock(); }
private void GlControlPaint(object sender, PaintEventArgs e) { if (!_loaded) { GL.ClearColor(Color4.LightSteelBlue); GL.Clear(ClearBufferMask.ColorBufferBit); } else if (!_bound) { GL.ClearColor(Color4.SteelBlue); GL.Clear(ClearBufferMask.ColorBufferBit); } else if (_screenCap) { _screenCap = false; SuspendLayout(); _graph.HideUI(); Size tmp = glControl.Size; glControl.Size = new Size(2000, 750); CanvasManager.ChangeViewportSize(glControl.Size); GL.ClearColor(Color4.White); GL.Clear(ClearBufferMask.ColorBufferBit); _graph.DrawGraph(); Bitmap bmp = new Bitmap(glControl.Width, this.glControl.Height); BitmapData data = bmp.LockBits(glControl.ClientRectangle, ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); GL.Finish(); GL.ReadPixels(0, 0, this.glControl.Width, this.glControl.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); bmp.RotateFlip(RotateFlipType.RotateNoneFlipY); bmp.Save(_screenCapFile, ImageFormat.Png); glControl.Size = tmp; CanvasManager.ChangeViewportSize(glControl.Size); _graph.ShowUI(); ResumeLayout(); } else { GL.ClearColor(Color4.White); GL.Clear(ClearBufferMask.ColorBufferBit); _graph.DrawGraph(); } glControl.SwapBuffers(); }