Screenshot in D3D9 mit C#



  • Ich versuche mit C# und D3D9 einen Screenshot zu machen - dabei setze ich auf das DX Tutorial auf - leider wift mit GetFrontBufferData immer eine Exception - hat jemand ein Idee was ich falsch mache?

    //-----------------------------------------------------------------------------
    // File: CreateDevice.cs
    //
    // Desc: This is the first tutorial for using Direct3D. In this tutorial, all
    //       we are doing is creating a Direct3D device and using it to clear the
    //       window.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //-----------------------------------------------------------------------------
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    using System.Runtime.InteropServices;
    
    namespace DeviceTutorial
    {
    
    	public class CreateDevice : Form
    	{
    
    		[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
    		public static extern IntPtr GetDesktopWindow();
    
    		// Our global variables for this project
    		Device device = null; // Our rendering device
    
    		public CreateDevice()
    		{
    			// Set the initial size of our form
    			this.ClientSize = new System.Drawing.Size(400,300);
    			// And it's caption
    			this.Text = "D3D Tutorial 01: CreateDevice";
    		}
    
    		public bool InitializeGraphics()
    		{
    			try
    			{
    				// Now let's setup our D3D stuff
    				PresentParameters presentParams = new PresentParameters();
    				presentParams.Windowed=true;
    				presentParams.SwapEffect = SwapEffect.Discard;
    				presentParams.PresentFlag = PresentFlag.LockableBackBuffer;
    				presentParams.BackBufferWidth = 1280;
    				presentParams.BackBufferHeight = 1024;
    				presentParams.MultiSample = MultiSampleType.None;
    				presentParams.DeviceWindowHandle = this.Handle;
    
    				device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
    
    				return true;
    			}
    			catch (DirectXException)
                { 
                    return false; 
                }
    		}
    		private void Render()
    		{
    			if (device == null) 
    				return;
    
    			//Clear the backbuffer to a blue color 
    			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
    			//Begin the scene
    			device.BeginScene();
    
    			// Rendering of scene objects can happen here
    
    			//End the scene
    			device.EndScene();
    
    			device.Present();
    		}
    		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    		{
    			this.Render(); // Render on painting
    		}
    		protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
    		{
    			if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
    				this.Close(); // Esc was pressed
    
    			if((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Back)
    			{
    				this.ScreenShot("dummy.bmp");
    			}
    		}
    
    		public void ScreenShot(string fileName) 
    		{ 
    			DisplayMode dm = device.DisplayMode; 
    
    			try 
    			{ 
    				                                                                   //  D3DFMT_A8R8G8B8
    				Surface surf = device.CreateOffscreenPlainSurface(dm.Width, dm.Height, Format.A8R8G8B8, Pool.Default); 
    				device.GetFrontBufferData(0, surf); 
    				SurfaceLoader.Save(fileName, ImageFileFormat.Bmp, surf); 
    				device.GetFrontBufferData(0, surf);
    
    				surf.Dispose();
    			} 
    			catch(Exception e) 
    			{ 
    				MessageBox.Show(this, e.ToString());
    			} 
    		} 
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		static void Main() 
    		{
    
                using (CreateDevice frm = new CreateDevice())
                {
                    if (!frm.InitializeGraphics()) // Initialize Direct3D
                    {
                        MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");
                        return;
                    }
                    frm.Show();
    
                    // While the form is still valid, render and process messages
                    while(frm.Created)
                    {
                        frm.Render();
                        Application.DoEvents();
                    }
                }
    		}
    	}
    }
    


  • Hui, ne Exception!? Dagegen solltest du was unternehmen.

    Bye, TGGC



  • du warte bloß - ich hab nen freund der kennt sich voll gut aus mit C# und der hat ein tutorial über directx gelesen und so und dann liefe ich dir ein c# d3d9 screenshot demo, das dich vor angst erzittern lassen wird und alles was du bisher programmiert hast in den schatten stellt 😉

    Hui, ne Exception!? Dagegen solltest du was unternehmen

    ach ich schmeiß die einfach weiter - soll sich doch das betriebssystem um den *** kümmern 😉



  • Hast es jetzt hingekriegt? Also wann eine Exception fliegt, müsste ja normal schon dokumentiert sein.


Anmelden zum Antworten