Disclaimer
This code is sample code that I wrote to help out a reader. It's not commented well, and it hasn't been extensively debugged. I post it here for illustrative purposes only. Hope you find it helpful.
This code shows how to render an image to the screen and then save the same image to a file.
The key to this sample is twofold:
You must set PresentationParameters.SwapEffect to SwapEffect.Copy orSwapEffect.Flip, otherwise the data in the back buffer will be garbage (that's whatSwapEffect.Discard means).
All the hard work is done by SurfaceLoader.Save.
using System; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace Wangdera.CaptureImage { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private Device _device; private Mesh _mesh; private System.Windows.Forms.Panel _canvas; private System.Windows.Forms.Label label1; private System.Windows.Forms.TrackBar _nudZoom; private System.Windows.Forms.Button _bSaveImage; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); InitializeGraphics(); } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this._canvas = new System.Windows.Forms.Panel(); this._nudZoom = new System.Windows.Forms.TrackBar(); this.label1 = new System.Windows.Forms.Label(); this._bSaveImage = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this._nudZoom)).BeginInit(); this.SuspendLayout(); // // _canvas // this._canvas.Dock = System.Windows.Forms.DockStyle.Top; this._canvas.Location = new System.Drawing.Point(0, 0); this._canvas.Name = "_canvas"; this._canvas.Size = new System.Drawing.Size(880, 464); this._canvas.TabIndex = 0; // // _nudZoom // this._nudZoom.Location = new System.Drawing.Point(120, 496); this._nudZoom.Maximum = -1; this._nudZoom.Minimum = -1000; this._nudZoom.Name = "_nudZoom"; this._nudZoom.TabIndex = 1; this._nudZoom.TickFrequency = 100; this._nudZoom.Value = -100; this._nudZoom.Scroll += new System.EventHandler(this._nudZoom_Scroll); // // label1 // this.label1.Location = new System.Drawing.Point(16, 496); this.label1.Name = "label1"; this.label1.TabIndex = 2; this.label1.Text = "&Zoom:"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // _bSaveImage // this._bSaveImage.Location = new System.Drawing.Point(248, 496); this._bSaveImage.Name = "_bSaveImage"; this._bSaveImage.Size = new System.Drawing.Size(120, 23); this._bSaveImage.TabIndex = 3; this._bSaveImage.Text = "&Save Image..."; this._bSaveImage.Click += new System.EventHandler(this._bSaveImage_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(880, 581); this.Controls.Add(this._bSaveImage); this.Controls.Add(this.label1); this.Controls.Add(this._nudZoom); this.Controls.Add(this._canvas); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this._nudZoom)).EndInit(); this.ResumeLayout(false); } #endregion protected bool InitializeGraphics() { PresentParameters pres = new PresentParameters(); pres.Windowed = true; pres.SwapEffect = SwapEffect.Copy; pres.EnableAutoDepthStencil = true; pres.AutoDepthStencilFormat = DepthFormat.D16; _device = new Device(0, DeviceType.Hardware, _canvas, CreateFlags.SoftwareVertexProcessing, pres); Material material = new Material(); material.Diffuse = Color.Yellow; material.Specular = Color.White; material.SpecularSharpness = 10.0F; _device.Material = material; _mesh = Mesh.Teapot(_device); SetupLights(); return true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); Render(); } protected void Render() { _device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.BlueViolet, 1.0F, 0); _device.BeginScene(); SetupMatrices(); _mesh.DrawSubset(0); _device.EndScene(); _device.Present(); } protected void SetupLights() { _device.RenderState.Lighting = true; _device.Lights[0].Diffuse = Color.White; _device.Lights[0].Specular = Color.White; _device.Lights[0].Type = LightType.Directional; _device.Lights[0].Direction = new Vector3(-1, -1, 3); _device.Lights[0].Update(); _device.Lights[0].Enabled = true; _device.RenderState.Ambient = Color.FromArgb(0x00, 0x00, 0x00); } protected void SetupMatrices() { _device.Transform.World = Matrix.Identity; _device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, _nudZoom.Value / 10.0F), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); _device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4.0F, 1.0F, 0.1F, 10000.0F); } private void _nudZoom_Scroll(object sender, System.EventArgs e) { Invalidate(); } private void _bSaveImage_Click(object sender, System.EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "JPEG (*.jpg)|*.jpg"; if (dialog.ShowDialog() == DialogResult.OK) { // Note that for this to work, you have to have SwapEffect.Flip or SwapEffect.Copy set in // the PresentationParameters set at device creation. SurfaceLoader.Save(dialog.FileName, ImageFileFormat.Jpg, _device.GetBackBuffer(0, 0, BackBufferType.Mono)); } } } }