当前位置: 首页>>代码示例>>C#>>正文


C# Texture2D.Dispose方法代码示例

本文整理汇总了C#中Microsoft.Xna.Framework.Graphics.Texture2D.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.Dispose方法的具体用法?C# Texture2D.Dispose怎么用?C# Texture2D.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Xna.Framework.Graphics.Texture2D的用法示例。


在下文中一共展示了Texture2D.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TakeScreenShot

        public static void TakeScreenShot(GraphicsDevice device, Keys theKey)
        {
            currentState = Keyboard.GetState();

            if (currentState.IsKeyDown(theKey) && preState.IsKeyUp(theKey))
            {
                byte[] screenData;

                screenData = new byte[device.PresentationParameters.BackBufferWidth * device.PresentationParameters.BackBufferHeight * 4];

                device.GetBackBufferData<byte>(screenData);

                Texture2D Screenshot = new Texture2D(device, device.PresentationParameters.BackBufferWidth, device.PresentationParameters.BackBufferHeight, false, device.PresentationParameters.BackBufferFormat);

                Screenshot.SetData<byte>(screenData);

                string name = "Screenshot_" + counter + ".jpeg";
                while (File.Exists(name))
                {
                    counter += 1;
                    name = "Screenshot_" + counter + ".jpeg";

                }

                Stream stream = new FileStream(name, FileMode.Create);

                Screenshot.SaveAsJpeg(stream, Screenshot.Width, Screenshot.Height);

                stream.Close();

                Screenshot.Dispose();
            }

            preState = currentState;
        }
开发者ID:JeoffreyW,项目名称:2dShooter,代码行数:35,代码来源:GameUtilities.cs

示例2: Image2Texture

        /// <summary>
        /// Converts the given System.Drawing.Image into a Texture2D.
        /// </summary>
        /// <param name="image">The image to be converted</param>
        /// <param name="graphicsDevice">The GraphicsDevice with which the image will be displayed</param>
        /// <param name="texture">A texture to reuse - can be null</param>
        /// <returns>A Texture2D of the image</returns>
        public static Texture2D Image2Texture(System.Drawing.Image image, GraphicsDevice graphicsDevice, Texture2D texture)
        {
            GraphicsDevice graphics = graphicsDevice;

            if (graphics == null) return null;

            if (image == null)
            {
                return null;
            }

            if (texture == null || texture.IsDisposed ||
                texture.Width != image.Width ||
                texture.Height != image.Height ||
                texture.Format != SurfaceFormat.Color)
            {
                if (texture != null && !texture.IsDisposed)
                {
                    texture.Dispose();
                }

                texture = new Texture2D(graphics, image.Width, image.Height, false, SurfaceFormat.Color);
            }
            else
            {
                for (int i = 0; i < 16; i++)
                {
                    if (graphics.Textures[i] == texture)
                    {
                        graphics.Textures[i] = null;
                        break;
                    }
                }
            }

            //Memory stream to store the bitmap data.
            MemoryStream ms = new MemoryStream();

            //Save to that memory stream.
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

            //Go to the beginning of the memory stream.
            ms.Seek(0, SeekOrigin.Begin);

            //Fill the texture.
            texture = Texture2D.FromStream(graphics, ms, image.Width, image.Height, false);

            //Close the stream.
            ms.Close();
            ms = null;

            return texture;
        }
开发者ID:crowcasso,项目名称:KinectExplorer,代码行数:60,代码来源:ShapesManager.cs

示例3: captureScreenshot

        private void captureScreenshot()
        {
            try
            {
                byte[] screenData;

                screenData = new byte[graphicsDevice.PresentationParameters.BackBufferWidth * graphicsDevice.PresentationParameters.BackBufferHeight * 4];
                //take whats on the graphics device and put it to the back buffer
                graphicsDevice.GetBackBufferData<byte>(screenData);
                Texture2D texture2D = new Texture2D(graphicsDevice,
                                                    graphicsDevice.PresentationParameters.BackBufferWidth,
                                                    graphicsDevice.PresentationParameters.BackBufferHeight, false,
                                                    graphicsDevice.PresentationParameters.BackBufferFormat);
                texture2D.SetData<byte>(screenData);
                saveScreenshot(texture2D);
                texture2D.Dispose();
            }
            catch (Exception e)
            {
                MessageBox.Show("You broke it....");
            }
        }
开发者ID:DuxClarus,项目名称:Uat-Portfolio,代码行数:22,代码来源:Screenshot.cs

示例4: PrintMap


//.........这里部分代码省略.........

            for (int y = 0; y < map.MapDimensions.Y; y++)
            {
                for (int x = 0; x < map.MapDimensions.X; x++)
                {
                    destinationRectangle.X = x * map.TileSize.X/2;
                    destinationRectangle.Y = y * map.TileSize.Y/2;

                    if (true)
                    {
                        Point mapPosition = new Point(x, y);
                        if (drawBase)
                        {
                            int baseLayerValue = map.GetBaseLayerValue(mapPosition) - 1;

                            Rectangle sourceRectangle =  new Rectangle(
                                (baseLayerValue % map.TilesPerRow) * map.TileSize.X/2,
                                (baseLayerValue / map.TilesPerRow) * map.TileSize.Y/2,
                                 map.TileSize.X / 2, map.TileSize.Y / 2);

                            if (sourceRectangle != Rectangle.Empty)
                            {
                                if ((x > 0 && x < 64) && (y > 0 && y < 64))
                                {
                                    byte[] tileData = new byte[sourceRectangle.Width * sourceRectangle.Height * 4];

                                    printTex.GetData(0, sourceRectangle, tileData, 0, tileData.Length);

                                    t2d.SetData<byte>(0, destinationRectangle, tileData, 0, tileData.Length);

                                }
                            }
                        }

                        if (drawFringe)
                        {
                            int fringeLayerValue = map.GetFringeLayerValue(mapPosition) - 1;

                            Rectangle sourceRectangle = new Rectangle(
                                (fringeLayerValue % map.TilesPerRow) * map.TileSize.X / 2,
                                (fringeLayerValue / map.TilesPerRow) * map.TileSize.Y / 2,
                                 map.TileSize.X / 2, map.TileSize.Y / 2);

                            if (sourceRectangle != Rectangle.Empty && fringeLayerValue >= 0)
                            {
                                if ((x > 0 && x < 64) && (y > 0 && y < 64))
                                {
                                    byte[] tileData = new byte[sourceRectangle.Width * sourceRectangle.Height * 4];

                                    printTex.GetData(0, sourceRectangle, tileData, 0, tileData.Length);

                                    t2d.SetData<byte>(0, destinationRectangle, tileData, 0, tileData.Length);

                                }
                            }
                        }

                        if (drawObject)
                        {
                            int objectLayerValue = map.GetObjectLayerValue(mapPosition) - 1;

                            Rectangle sourceRectangle = new Rectangle(
                                (objectLayerValue % map.TilesPerRow) * map.TileSize.X / 2,
                                (objectLayerValue / map.TilesPerRow) * map.TileSize.Y / 2,
                                 map.TileSize.X / 2, map.TileSize.Y / 2);

                            if (sourceRectangle != Rectangle.Empty && objectLayerValue >= 0)
                            {
                                if ((x > 0 && x < 64) && (y > 0 && y < 64))
                                {
                                    byte[] tileData = new byte[sourceRectangle.Width * sourceRectangle.Height * 4];

                                    printTex.GetData(0, sourceRectangle, tileData, 0, tileData.Length);

                                    t2d.SetData<byte>(0, destinationRectangle, tileData, 0, tileData.Length);

                                }
                            }
                        }
                    }
                }
            }

            int i = 0;
            string name = "ScreenShot" + i.ToString() + ".png";
            while (File.Exists(name))
            {
                i += 1;
                name = "ScreenShot" + i.ToString() + ".png";

            }

            Stream st = new FileStream(name, FileMode.Create);

            t2d.SaveAsPng(st, t2d.Width, t2d.Height);

            st.Close();

            t2d.Dispose();
        }
开发者ID:plhearn,项目名称:pet,代码行数:101,代码来源:TileEngine.cs

示例5: Render

        public override void Render(GraphicsDevice graphicsDevice)
        {
            Update();

            if (visible&&size.Width>0&&size.Height>0)
            {
                Color drawColour = new Color(1f, 1f, 1f);

                if (!enabled)
                {
                    drawColour = new Color(.5f, .5f, .5f);
                }
                //Generate 1px white texture
                Texture2D shade = new Texture2D(graphicsDevice, 1,1,1,TextureUsage.None,SurfaceFormat.Color);
                shade.SetData(new Color[] { Color.White });
                //Draw end boxes
                SpriteBatch spriteBatch = new SpriteBatch(graphicsDevice);
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
                spriteBatch.Draw(shade, new Rectangle(size.X, size.Y, size.Height, size.Height), drawColour);
                spriteBatch.Draw(shade, new Rectangle(size.X + size.Width - size.Height, size.Y, size.Height, size.Height), drawColour);
                
                //Draw line
                float sliderPercent = getPercent();
                int sliderPartialWidth = size.Height / 4;
                int midHeight = (int)(size.Height/2)-1;
                int actualWidth = size.Width - 2 * size.Height;
                int actualPosition = (int)(sliderPercent * actualWidth);
                spriteBatch.Draw(shade, new Rectangle(size.X, size.Y + midHeight, size.Width, 1), drawColour);

                //Draw slider
                spriteBatch.Draw(shade, new Rectangle(size.X + size.Height + actualPosition - sliderPartialWidth, size.Y + midHeight - sliderPartialWidth, size.Height / 2, size.Height / 2), drawColour);
                if (text != "")
                {
                    //Draw text
                    spriteBatch.DrawString(uiFont, text, new Vector2(size.X, size.Y - 36), drawColour);
                }
                //Draw amount
                spriteBatch.DrawString(uiFont, (((float)(int)(value * 10)) / 10).ToString(), new Vector2(size.X, size.Y - 20), drawColour); 
                
                spriteBatch.End();
                shade.Dispose();
            }
        }
开发者ID:GlennSandoval,项目名称:Infiniminer,代码行数:43,代码来源:InterfaceSlider.cs

示例6: destroyContent

 public void destroyContent(Texture2D texture)
 {
     texture.Dispose();
 }
开发者ID:griffenclaw,项目名称:Farm,代码行数:4,代码来源:AssetManager.cs

示例7: SetTexture

        protected void SetTexture(Texture2D tex)
        {
            lock (this)
            {
                //Trace.WriteLine("SetTexture: " + this.Filename.ToString());

                if (IsDisposed && tex != null)
                {
                    tex.Dispose();
                    Global.RemoveTexture(tex);
                    tex = null;
                }

                if (BodyRequestState != null)
                {
                    this.BodyRequestState.Dispose();
                    this.BodyRequestState = null;
                }

                this._Result = tex;
                this.FinishedReading = true;
                graphicsDevice = null;

                if(!IsDisposed)
                    DoneEvent.Set();
            }
        }
开发者ID:abordt,项目名称:Viking,代码行数:27,代码来源:TextureReader.cs

示例8: Update

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // Get input from the player
            inputController.Update();
            // screencaps
            if (inputController.ScreenCap.JustPressed)
            {
                Draw(gameTime);
                Texture2D texture = new Texture2D(graphicsDevice, GraphicsConstants.VIEWPORT_WIDTH, GraphicsConstants.VIEWPORT_HEIGHT);
                Color[] data = new Color[texture.Width * texture.Height];
                graphicsDevice.GetBackBufferData<Color>(data);
                texture.SetData<Color>(data);
                Stream stream = File.OpenWrite("output.png");
                texture.SaveAsPng(stream, texture.Width, texture.Height);
                stream.Dispose();
                texture.Dispose();
            }

            if (currentContext.Exit || currentContext.NextContext != null)
            {
                targetOverlayAlpha = 1;
                if (currentOverlayAlpha == 1)
                {
                    //audioPlayer.StopSong();
                    if (currentContext.Exit)
                        exitGame = true;
                    else if (!asyncStarted)
                    {
                        asyncStarted = true;
                        asyncFinished = false;
                        InitializeContextComponents(currentContext.NextContext);
                        asyncFinished = true;
                    }
                    if (asyncStarted && asyncFinished)
                    {
                        //Thread.Sleep(1000);
                        asyncStarted = false;
                        targetOverlayAlpha = 0;
                        currentContext.Dispose();
                        currentContext = currentContext.NextContext;
                    }
                }
            }
            else
            {
                currentContext.Update(gameTime);
            }

            currentOverlayAlpha = MathHelper.Lerp(currentOverlayAlpha, targetOverlayAlpha, currentContext.FadeMultiplier);
            //Console.WriteLine(Math.Abs(currentOverlayAlpha - targetOverlayAlpha));
            if (Math.Abs(currentOverlayAlpha - targetOverlayAlpha) < 0.001f || inputController.Zoom.Pressed)
                currentOverlayAlpha = targetOverlayAlpha;
        }
开发者ID:kjin,项目名称:TubeRacer,代码行数:58,代码来源:ContextManager.cs

示例9: onBeginTransition

		public override IEnumerator onBeginTransition()
		{
			// create a single pixel transparent texture so we can do our squares out to the next scene
			_overlayTexture = Graphics.createSingleColorTexture( 1, 1, Color.Transparent );

			// populate squares
			yield return Core.startCoroutine( tickEffectProgressProperty( _squaresEffect, squaresInDuration, easeType ) );

			// load up the new Scene
			yield return Core.startCoroutine( loadNextScene() );

			// dispose of our previousSceneRender. We dont need it anymore.
			previousSceneRender.Dispose();
			previousSceneRender = null;

			// delay
			yield return Coroutine.waitForSeconds( delayBeforeSquaresInDuration );

			// unpopulate squares
			yield return Core.startCoroutine( tickEffectProgressProperty( _squaresEffect, squaresInDuration, EaseHelper.oppositeEaseType( easeType ), true ) );

			transitionComplete();

			// cleanup
			_overlayTexture.Dispose();
			Core.content.unloadEffect( _squaresEffect.Name );
		}
开发者ID:prime31,项目名称:Nez,代码行数:27,代码来源:SquaresTransition.cs

示例10: Unload

 /// <summary>
 /// Unload and dispose by object.
 /// </summary>
 /// <param name="source"></param>
 public void Unload(Texture2D source)
 {
     Textures.Remove(source);
     source.Dispose();
 }
开发者ID:bLeenstra,项目名称:DictatorRts,代码行数:9,代码来源:GraphicHandler.cs

示例11: ToBitmap

        public static drawing.Bitmap ToBitmap(Texture2D texture)
        {
            drawing.Bitmap bitmap = new drawing.Bitmap(texture.Width, texture.Height, drawing.Imaging.PixelFormat.Format32bppArgb);

            byte blue;
            IntPtr safePtr;
            drawing.Imaging.BitmapData bitmapData;
            drawing.Rectangle rect = new drawing.Rectangle(0, 0, texture.Width, texture.Height);
            byte[] textureData = new byte[4 * texture.Width * texture.Height];

            texture.GetData<byte>(textureData);
            for (int i = 0; i < textureData.Length; i += 4)
            {
                blue = textureData[i];
                textureData[i] = textureData[i + 2];
                textureData[i + 2] = blue;
            }
            bitmapData = bitmap.LockBits(rect, drawing.Imaging.ImageLockMode.WriteOnly, drawing.Imaging.PixelFormat.Format32bppArgb);
            safePtr = bitmapData.Scan0;
            Marshal.Copy(textureData, 0, safePtr, textureData.Length);
            bitmap.UnlockBits(bitmapData);

            textureData = null;
            texture.Dispose();

            return bitmap;
        }
开发者ID:JacquesLucke,项目名称:Collage,代码行数:27,代码来源:Utils.cs

示例12: GenerateNoiseMap

        static void GenerateNoiseMap(int width, int height, ref Texture2D noiseTexture, int octaves)
        {
            var data = new float[width*height];

            /// track min and max noise value. Used to normalize the result to the 0 to 1.0 range.
            var min = float.MaxValue;
            var max = float.MinValue;

            /// rebuild the permutation table to get a different noise pattern.
            /// Leave this out if you want to play with changing the number of octaves while
            /// maintaining the same overall pattern.
            Noise2d.Reseed();

            var frequency = 10.0f;
            var amplitude = 200.0f;

            for (var octave = 0; octave < octaves; octave++)
            {
                /// parallel loop - easy and fast.
                Parallel.For(0
                    , width * height
                    , (offset) =>
                    {
                        var i = offset % width;
                        var j = offset / width;
                        var noise = Noise2d.Noise(i * frequency * 1f / width, j * frequency * 1f / height);
                        noise = data[j * width + i] += noise * amplitude;

                        min = Math.Min(min, noise);
                        max = Math.Max(max, noise);

                    }
                );

                frequency *= 2;
                amplitude /= 2;
            }

            if (noiseTexture != null && (noiseTexture.Width != width || noiseTexture.Height != height))
            {
                noiseTexture.Dispose();
                noiseTexture = null;
            }
            if (noiseTexture == null)
            {
                noiseTexture = new Texture2D(device, width, height, false, SurfaceFormat.Color);
            }

            var colors = data.Select(
                (f) =>
                {
                    var norm = (f - min) / (max - min);
                    return new Color(norm, norm, norm, 1);
                }
            ).ToArray();

            noiseTexture.SetData(colors);
        }
开发者ID:itsarabbit,项目名称:TearingSimulator2002,代码行数:58,代码来源:NoiseManager.cs

示例13: Dispose

 private static void Dispose(Texture2D sprite)
 {
     if (sprite != null)
     sprite.Dispose();
 }
开发者ID:colincapurso,项目名称:IC2013,代码行数:5,代码来源:Sprites.cs

示例14: trimTransparentEdges


//.........这里部分代码省略.........
                            return lastTransparentColumn;
                        }
                    }
                    lastTransparentColumn = i;
                }

                return lastTransparentColumn;
            };
            Func<int, int> findTransparentColumnFromRight = (end) =>
            {
                int lastTransparentColumn = canvas.Width - 1;
                for (int i = canvas.Width - 1; i >= end; i--)
                {
                    for (int j = 0; j < canvas.Height; j++)
                    {
                        if (data2d[i, j].A > 0)
                        {
                            return lastTransparentColumn;
                        }
                    }
                    lastTransparentColumn = i;
                }

                return lastTransparentColumn;
            };
            Func<int, int> findTransparentRowFromTop = (end) =>
            {
                int lastTransparentRow = 0;
                for (int j = 0; j < end; j++)
                {
                    for (int i = 0; i < canvas.Width; i++)
                    {
                        if (data2d[i, j].A > 0)
                        {
                            return lastTransparentRow;
                        }
                    }
                    lastTransparentRow = j;
                }

                return lastTransparentRow;
            };
            Func<int, int> findTransparentRowFromBottom = (end) =>
            {
                int lastTransparentRow = canvas.Height - 1;
                for (int j = canvas.Height - 1; j >= end; j--)
                {
                    for (int i = 0; i < canvas.Width; i++)
                    {
                        if (data2d[i, j].A > 0)
                        {
                            return lastTransparentRow;
                        }
                    }
                    lastTransparentRow = j;
                }

                return lastTransparentRow;
            };

            canvas.GetData<Color>(data1d);
            for (int i = 0; i < canvas.Width; i++)
            {
                for (int j = 0; j < canvas.Height; j++)
                {
                    data2d[i, j] = data1d[i + j * canvas.Width];
                }
            }

            lastTransparentColumnFromLeft = findTransparentColumnFromLeft(halfWidth);
            lastTransparentColumnFromRight = findTransparentColumnFromRight(halfWidth);
            lastTransparentRowFromTop = findTransparentRowFromTop(halfHeight);
            lastTransparentRowFromBottom = findTransparentRowFromBottom(halfHeight);

            widthTrim = Math.Min(lastTransparentColumnFromLeft, (canvas.Width - 1) - lastTransparentColumnFromRight);
            heightTrim = Math.Min(lastTransparentRowFromTop, (canvas.Height - 1) - lastTransparentRowFromBottom);
            newWidth = canvas.Width - (widthTrim * 2);
            newHeight = canvas.Height - (heightTrim * 2);

            if (newWidth < 1 || newHeight < 1)
            {
                canvas.Dispose();
                canvas = new Texture2D(_graphicsDevice, 1, 1);
                canvas.SetData<Color>(new[] { Color.Transparent });
            }

            newData = new Color[newWidth * newHeight];
            for (int i = 0; i < newWidth; i++)
            {
                for (int j = 0; j < newHeight; j++)
                {
                    newData[i + j * newWidth] = data2d[widthTrim + i, heightTrim + j];
                }
            }
            canvas.Dispose();
            canvas = new Texture2D(_graphicsDevice, newWidth, newHeight);
            canvas.SetData<Color>(newData);

            return canvas;
        }
开发者ID:klutch,项目名称:StasisEngine,代码行数:101,代码来源:MaterialRenderer.cs

示例15: SaveImage

        public void SaveImage(string path)
        {
            Texture2D t = new Texture2D(Renderer.GD, (int)Size.X, (int)Size.Y);

            Color[] data = new Color[(int)Size.X * (int)Size.Y];
            int i = 0;

            for (int y = 0; y < (int)Size.Y; y++)
            {
                for (int x = 0; x < (int)Size.X; x++)
                {
                    if (GetCostArray()[x, y] == 0)
                        data[i++] = new Color(0, 0, 0);
                    else
                    {
                        data[i++] = new Color(255, 255, 255);
                    }
                }
            }

            t.SetData<Color>(data);

            DateTime date = DateTime.Now; //Get the date for the file name
            if(!Directory.Exists(path + "/Mapa "))
            {
                Directory.CreateDirectory(path + "/Mapa ");
            }
            Stream stream = File.Create(path + "/Mapa " + date.ToString("dd-MM-yy H_mm_ss") + ".png");

            //Save as PNG
            t.SaveAsPng(stream, (int)Size.X, (int)Size.Y);
            Logger.Write("Saved map png");
            stream.Dispose();
            t.Dispose();
        }
开发者ID:BartoszF,项目名称:ArtifactsRider,代码行数:35,代码来源:Chunk.cs


注:本文中的Microsoft.Xna.Framework.Graphics.Texture2D.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。