public FormatConvertedBitmap GetFrame() { lock (Gl) { // Render. Gl.Blit(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; FormatConvertedBitmap newFormatedBitmapSource = null; switch (RenderContextType) { case RenderContextType.DIBSection: { var provider = Gl.RenderContextProvider as DIBSectionRenderContextProvider; //hBitmap = provider.DIBSection.HBitmap; //break; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); break; } case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = Gl.RenderContextProvider as FBORenderContextProvider; //hBitmap = provider.InternalDIBSection.HBitmap; //break; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); break; } } //Bitmap res = hBitmap != null ? Bitmap.FromHbitmap(hBitmap) : null; //return res; return(newFormatedBitmapSource); } }
private void UpdateImage() { FBORenderContextProvider provider = GL.RenderContextProvider as FBORenderContextProvider; IntPtr hBitmap = provider.InternalDIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { FormatConvertedBitmap formattedBitmap = new FormatConvertedBitmap(); formattedBitmap.BeginInit(); formattedBitmap.Source = HBitmapToBitmapSource(hBitmap); formattedBitmap.DestinationFormat = PixelFormats.Rgb24; formattedBitmap.EndInit(); _image.Source = formattedBitmap; } }
/// <summary> /// Handles the Tick event of the timer control. /// </summary> private void TimerTick() { try { if (this.Dispatcher.HasShutdownStarted || this.Dispatcher.HasShutdownFinished) { this.timer.Abort(); return; } this.Dispatcher.Invoke( () => { // Lock on OpenGL lock (this.OpenGL) { // Start the stopwatch so that we can time the rendering this.stopwatch.Restart(); // Make GL current this.OpenGL.MakeCurrent(); // If there is a draw handler, then call it OpenGLEventHandler handler = this.OpenGLDraw; if (handler != null) { handler(this, this.eventArgsFast); } else { this.OpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT); } // Draw the FPS if (this.DrawFPS) { this.OpenGL.DrawText(5, 5, 1.0f, 0.0f, 0.0f, "Courier New", 12.0f, string.Format("Draw Time: {0:0.0000} ms ~ {1:0.0} FPS", this.frameTime, this.currentFrameRate)); this.OpenGL.Flush(); } // Render this.OpenGL.Blit(IntPtr.Zero); switch (this.RenderContextType) { case RenderContextType.DIBSection: { DIBSectionRenderContextProvider provider = this.OpenGL.RenderContextProvider as DIBSectionRenderContextProvider; IntPtr hBitmap = provider.DIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { FormatConvertedBitmap newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap); // Copy the pixels over this.image.Source = newFormatedBitmapSource; } } break; case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = this.OpenGL.RenderContextProvider as FBORenderContextProvider; IntPtr hBitmap = provider.InternalDIBSection.HBitmap; if (hBitmap != IntPtr.Zero) { FormatConvertedBitmap newFormatedBitmapSource = GetFormatedBitmapSource(hBitmap); // Copy the pixels over this.image.Source = newFormatedBitmapSource; } } break; default: break; } // Stop the stopwatch this.stopwatch.Stop(); // Store the frame time this.frameTime = this.stopwatch.Elapsed.TotalMilliseconds; } }, DispatcherPriority.Send); if (this.frameRate > 0.0) { Thread.Sleep(new TimeSpan(0, 0, 0, 0, (int)(Math.Max(1000.0 - this.frameTime, this.frameRate) / this.frameRate))); } } catch (Exception ex) { ex.ToString(); } }
public ImageSource GetFrame() { if (!BlitImage) { return(ImgSource); } lock (Gl) { Gl.Blit(IntPtr.Zero); IntPtr hBitmap = IntPtr.Zero; switch (RenderContextType) { case RenderContextType.DIBSection: { DIBSectionRenderContextProvider provider = Gl.RenderContextProvider as DIBSectionRenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.DIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. return(newFormatedBitmapSource); } case RenderContextType.NativeWindow: break; case RenderContextType.HiddenWindow: break; case RenderContextType.FBO: { FBORenderContextProvider provider = Gl.RenderContextProvider as FBORenderContextProvider; // TODO: We have to remove the alpha channel - for some reason it comes out as 0.0 // meaning the drawing comes out transparent. FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap(); newFormatedBitmapSource.BeginInit(); newFormatedBitmapSource.Source = BitmapConversion.HBitmapToBitmapSource(provider.InternalDIBSection.HBitmap); newFormatedBitmapSource.DestinationFormat = PixelFormats.Rgb24; newFormatedBitmapSource.EndInit(); // Copy the pixels over. return(newFormatedBitmapSource); } case RenderContextType.PBO: { PBORenderContextProvider provider = Gl.RenderContextProvider as PBORenderContextProvider; var width = provider.Width; var height = provider.Height; var pixelsPtr = provider.PixelPtr; if (pixelsPtr == IntPtr.Zero) { return(null); } var size = provider.Size; var stride = provider.Stride; var rect = new Int32Rect(0, 0, width, height); WriteableBitmap.WritePixels(rect, pixelsPtr, size, stride, 0, 0); return(WriteableBitmap); } default: break; } } return(null); }