public Texture TakeScreenshot() { DisplayMode dm = device.DisplayMode; Microsoft.DirectX.Direct3D.Texture shot = new Microsoft.DirectX.Direct3D.Texture(device, dm.Width, dm.Height, 1, 0, Format.A8R8G8B8, Pool.SystemMemory); device.GetFrontBufferData(0, shot.GetSurfaceLevel(0)); System.Drawing.Rectangle rect; if (fullscreen) { rect = new Rectangle(0, 0, width, height); } else { rect = new Rectangle(Kernel.Form.PointToScreen(new System.Drawing.Point(0, 0)), new Size(width, height)); } int[] pixels = (int[])shot.LockRectangle(typeof(int), 0, LockFlags.None, dm.Width * dm.Height); Texture res = new Texture(width, height); int i = 0; for (int y = rect.Top; y < rect.Bottom; y++) { for (int x = rect.Left; x < rect.Right; x++) { res[i++] = Color.FromColorCode(pixels[x + y * dm.Width]); } } shot.UnlockRectangle(0); return(res); }
//Loads the texture into a sprite private void SetTextureToScreen() { // Load texture from other sprite's texture if (this.ParentSprite == null) { uint f = 0; //Frame of animation uint x = 0; //Frame x reltive to frame uint y = 0; //Frame y reletive to frame uint SourceXStart = 0; //Frame's starting x reletive to sprite sheet uint SourceYStart = 0; //Frame's starting y reletive to sprite sheet uint DestinationY = 0; uint DestinationX = 0; uint AnimsPerSheetWidth = (uint)(this.SheetWidth / this.Width); //Load the whole texture till we can chop it up Direct3D.Texture SpriteSheet = Direct3D.TextureLoader.FromFile ( this.ParentDevice, this.Image, 0, 0, 0, Direct3D.Usage.None, Direct3D.Format.A8B8G8R8, Direct3D.Pool.Managed, Direct3D.Filter.Linear, Direct3D.Filter.Linear, this.MaskColor.ToArgb() ); //Chop this sprite sheet up into frames //Create a blank bitmap and use it as a template for creating a texture System.Drawing.Bitmap SingleFrameBackground = new System.Drawing.Bitmap(this.Width, this.Height); uint[,] DestinationPixels; uint[,] SourcePixels = (uint[, ])SpriteSheet.LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.SheetHeight, this.SheetWidth), Direct3D.LockFlags.None, this.SheetHeight, this.SheetWidth); this.textures = new Direct3D.Texture[this.AnimCount]; //For each frame of animation for (f = 0; f < this.AnimCount; f++) { //Get this frame's starting y if (f >= AnimsPerSheetWidth) { SourceYStart = (uint)(f / AnimsPerSheetWidth); } //Get this frame's starting x SourceXStart = (uint)(f - (SourceYStart * AnimsPerSheetWidth)); SourceYStart *= (uint)this.Height; SourceXStart *= (uint)this.Width; DestinationY = 0; DestinationX = 0; //Create the texture's background this.textures[f] = new Direct3D.Texture(this.ParentDevice, SingleFrameBackground, Direct3D.Usage.None, Direct3D.Pool.Managed); DestinationPixels = (uint[, ]) this.textures[f].LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.Height, this.Width), Direct3D.LockFlags.None, this.Height, this.Width); //For this frame's x for (x = SourceXStart; x < SourceXStart + this.Width; x++) { //For this frame's y for (y = SourceYStart; y < SourceYStart + this.Height; y++) { DestinationPixels[DestinationY, DestinationX] = SourcePixels[y, x]; DestinationY++; } DestinationX++; DestinationY = 0; } this.textures[f].UnlockRectangle(0); } SpriteSheet.UnlockRectangle(0); SpriteSheet.Dispose(); //Create masks for each frame of animation this.CollisionRects = new CollisionRectAccess(this.ParentDevice, this.textures, this.Verticies); } }