示例#1
0
        static void Main(string[] args)
        {
            InitGraphics();

            for (int i = 0; i < args.Length; ++i)
            {
                string arg = args[i];
                arg = arg.Substring(arg.LastIndexOf('\\') + 1, arg.Length - arg.LastIndexOf('\\') - 1);
                if (File.Exists(arg))
                {
                    try
                    {
                        Texture2D src = Texture2D.FromStream(GraphicsDevice, File.OpenRead(arg));
                        Texture2D dest = new Texture2D(GraphicsDevice, src.Width >> 1, src.Height >> 1);
                        Color[] src_pix = new Color[src.Width * src.Height];
                        Color[] dest_pix = new Color[dest.Width * dest.Height];
                        src.GetData<Color>(src_pix);
                        for (int j = 0; j < dest.Width; ++j)
                        {
                            for (int k = 0; k < dest.Height; ++k)
                            {
                                dest_pix[(k * dest.Width) + j] = src_pix[((k<<1) * src.Width) + (j<<1)];
                            }
                        }
                        dest.SetData<Color>(dest_pix);
                        dest.SaveAsJpeg(File.OpenWrite("h_" + arg), dest.Width, dest.Height);
                    }
                    catch (Exception e)
                    {

                    }
                }
            }
        }
示例#2
0
        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;
        }
示例#3
0
        /// <summary>
        /// Stores the current contents of the back buffer as a .PNG file.
        /// </summary>
        /// <param name="inGraphicsDevice">GraphicsDevice to grab the back buffer data from.</param>
        /// <param name="inFilename">String containing the name of the file to save.</param>
        /// <returns>True if the file is saved successfully, otherwise false.</returns>
        public static bool Capture(GraphicsDevice inGraphicsDevice,
                                   String inFilename, ScreenCaptureType inCaptureExtension)
        {
            if (inCaptureExtension == ScreenCaptureType.UseJPEG) { inFilename = inFilename + ".jpg"; }
            if (inCaptureExtension == ScreenCaptureType.UsePNG) { inFilename = inFilename + ".png"; }

            // Store the current BackBuffer data in a new array of Color values. This
            // will take what is currently on the BackBuffer; everything current being
            // drawn to the screen.
            Color[] colorData = new Color[inGraphicsDevice.Viewport.Width *
                                          inGraphicsDevice.Viewport.Height];
            inGraphicsDevice.GetBackBufferData<Color>(colorData);

            // Next set the colors into a Texture, ready for saving.
            Texture2D backBufferTexture = new Texture2D(inGraphicsDevice,
                                                        inGraphicsDevice.Viewport.Width,
                                                        inGraphicsDevice.Viewport.Height);
            backBufferTexture.SetData<Color>(colorData, 0, colorData.Length);

            // Create the file after checking whether it exists. This requires a means
            // of altering the intended filename so that it cannot overwrite an existing
            // screen-capture, for instance suffixing an incremental digit onto the file
            // name, but this would have to be saved to avoid overwritten files when the
            // game crashes and the count is lost.
            if (!File.Exists(inFilename))
            {
                using (FileStream fileStream = File.Create(inFilename))
                {
                    // The choice passed in as the 3rd parameter just exists for the sake
                    // of providing options. If one is clearly advantageous to the other
                    // I'll hard-code it to use that instead. But, for now, we allow the
                    // choice between JPEG and PNG. PNG files have transparency.
                    switch (inCaptureExtension)
                    {
                        case ScreenCaptureType.UseJPEG:
                            backBufferTexture.SaveAsJpeg(fileStream,
                                                         inGraphicsDevice.Viewport.Width,
                                                         inGraphicsDevice.Viewport.Height);
                            break;
                        case ScreenCaptureType.UsePNG:
                            backBufferTexture.SaveAsPng(fileStream,
                                                        inGraphicsDevice.Viewport.Width,
                                                        inGraphicsDevice.Viewport.Height);
                            break;
                    }

                    fileStream.Flush();
                }

                return true;
            }

            return false;
        }
示例#4
0
        public static BitmapImage Texture2Image(Texture2D texture)
        {
            try
            {
                if (texture == null)
                {
                    return null;
                }

                if (texture.IsDisposed)
                {
                    return null;
                }

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

                //Save the texture to the stream.
                texture.SaveAsJpeg(ms, texture.Width, texture.Height);

                ms.Seek(0, SeekOrigin.Begin);

                byte[] bytes = ms.ToArray();

                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(bytes);
                bi.EndInit();

                //Close the stream, we nolonger need it.
                ms.Close();
                ms = null;

                return bi;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
示例#5
0
        public static bool CaptureJpeg(GraphicsDevice inGraphicsDevice, String inFilename)
        {
            inFilename = inFilename + ".jpeg";

            // Store the current BackBuffer data in a new array of Color values. This
            // will take what is currently on the BackBuffer; everything current being
            // drawn to the screen.
            Color[] colorData = new Color[inGraphicsDevice.Viewport.Width *
                                          inGraphicsDevice.Viewport.Height];
            inGraphicsDevice.GetBackBufferData<Color>(colorData);

            // Next set the colors into a Texture, ready for saving.
            Texture2D backBufferTexture = new Texture2D(inGraphicsDevice,
                                                        inGraphicsDevice.Viewport.Width,
                                                        inGraphicsDevice.Viewport.Height);
            backBufferTexture.SetData<Color>(colorData, 0, colorData.Length);

            // Create the file after checking whether it exists. This requires a means
            // of altering the intended filename so that it cannot overwrite an existing
            // screen-capture, for instance suffixing an incremental digit onto the file
            // name, but this would have to be saved to avoid overwritten files when the
            // game crashes and the count is lost.
            if (!File.Exists(inFilename))
            {
                using (FileStream fileStream = File.Create(inFilename))
                {
                    backBufferTexture.SaveAsJpeg(fileStream,
                             inGraphicsDevice.Viewport.Width,
                             inGraphicsDevice.Viewport.Height);

                    fileStream.Flush();
                }

                return true;
            }

            return false;
        }
示例#6
0
 internal static byte[] Texture2DToBytes(Texture2D thumb)
 {
     MemoryStream s = new MemoryStream();
     thumb.SaveAsJpeg(s, thumb.Width, thumb.Height);
     byte[] data = new byte[s.Length];
     s.Position = 0;
     s.Read(data, 0, data.Length);
     s.Dispose();
     return data;
 }
示例#7
0
 private void Save(Texture2D texture, string name)
 {
     using (Stream stream = File.Create(name))
         texture.SaveAsJpeg(stream, texture.Width, texture.Height);
 }
示例#8
0
        private void ProcessImage( string outputDir )
        {
            string outfileBase = outputDir + @"\" + txtOutFileBaseName.Text;
            Rectangle frame = new Rectangle( 0, 0, (int)nudCutWidth.Value, (int)nudCutHeight.Value );
            Texture2D workingImg = new Texture2D( gd, frame.Width, frame.Height );
            int count = 0;

            int numRows = originalImg.Height / frame.Height;
            int numCols = originalImg.Width / frame.Width;
            int numPixels = frame.Width * frame.Height;

            for ( int i = 0; i < numRows; i++ )
            {
                frame.Y = i * frame.Height;
                for ( int j = 0; j < numCols; j++ )
                {
                    frame.X = j * frame.Width;

                    Color[] cpy = new Color[numPixels];
                    originalImg.GetData<Color>( 0, frame, cpy, 0, numPixels );
                    workingImg.SetData<Color>( cpy );

                    // Save the image
                    Stream s;
                    switch ( cmbOutFileType.SelectedIndex )
                    {
                        case 0:
                            s = File.OpenWrite( outfileBase + count.ToString() + ".png" );
                            workingImg.SaveAsPng( s, workingImg.Width, workingImg.Height );
                            s.Close();
                            break;
                        case 1:
                            s = File.OpenWrite( outfileBase + count.ToString() + ".jpg" );
                            workingImg.SaveAsJpeg( s, workingImg.Width, workingImg.Height );
                            s.Close();
                            break;
                        default:
                            break;
                    }

                    count++;
                }
            }

            MessageBox.Show( "Created " + count.ToString() + " tiles!", "Processing Done!" );
        }
示例#9
0
 protected void SaveTexture(Texture2D texture, string name)
 {
     FileStream stream = new FileStream(name, FileMode.Create);
     texture.SaveAsJpeg(stream, texture.Width, texture.Height);
     stream.Close();
 }
        private void MakeScreenshot()
        {
            try
            {
                //NOTE: This doesn't always work on all cards, especially if
                // desktop mode switches in fullscreen mode!

                screenshotNum++;
                // Make sure screenshots directory exists
                if (Directory.Exists(Directories.ScreenshotsDirectory) == false)
                    Directory.CreateDirectory(Directories.ScreenshotsDirectory);

                int width = BaseGame.Device.PresentationParameters.BackBufferWidth;
                int height = BaseGame.Device.PresentationParameters.BackBufferHeight;

                using (
                    var tex = new Texture2D(BaseGame.Device, width, height, false,
                                            BaseGame.Device.PresentationParameters.BackBufferFormat))
                {
                    int[] backbuffer = new int[width*height];
                    BaseGame.Device.GetBackBufferData(backbuffer);

                    tex.SetData(backbuffer);

                    FileHelper.StorageContainerMRE.WaitOne();
                    FileHelper.StorageContainerMRE.Reset();
                    // Open a storage container
                    StorageDevice storageDevice = FileHelper.XnaUserDevice;
                    if ((storageDevice != null) && storageDevice.IsConnected)
                    {
                        IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null);

                        async.AsyncWaitHandle.WaitOne();

                        using (StorageContainer container = storageDevice.EndOpenContainer(async))
                        {
                            async.AsyncWaitHandle.Close();
                            using (Stream stream = container.CreateFile(ScreenshotNameBuilder(screenshotNum)))
                            {
                                tex.SaveAsJpeg(stream, width, height);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write("Failed to save Screenshot: " + ex.ToString());
            }
        }
示例#11
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _screens.PrepareDraw();
            _screens.Draw(gameTime);

            if (DisplayUI)
                _ui.Draw(gameTime);

            //var model = Content.Load<Model>("Sponza");

            //GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            //GraphicsDevice.BlendState = BlendState.Opaque;
            //GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            //GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
            //GraphicsDevice.SamplerStates[1] = SamplerState.LinearWrap;
            //GraphicsDevice.SamplerStates[2] = SamplerState.LinearWrap;

            //// Calculate aspect ratio
            //float aspectRatio = (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height;

            //// Animated the model rotating
            //float modelRotation = (float)gameTime.TotalGameTime.TotalSeconds / 5.0f;

            //// Set the positions of the camera in world space, for our view matrix.
            //Vector3 cameraPosition = new Vector3(0.0f, 50.0f, -200.0f);
            //Vector3 lookAt = new Vector3(0.0f, 40.0f, 300.0f);

            //// Copy any parent transforms.
            //Matrix[] transforms = new Matrix[model.Bones.Count];
            //model.CopyAbsoluteBoneTransformsTo(transforms);

            //// Draw the model. A model can have multiple meshes, so loop.
            //foreach (ModelMesh mesh in model.Meshes)
            //{
            //    // This is where the mesh orientation is set,
            //    // as well as our camera and projection.
            //    foreach (BasicEffect effect in mesh.Effects)
            //    {
            //        effect.EnableDefaultLighting();
            //        effect.World =
            //            Matrix.CreateScale(1) *
            //            transforms[mesh.ParentBone.Index] *
            //            Matrix.CreateRotationY(modelRotation);
            //        effect.View = Matrix.CreateLookAt(cameraPosition, lookAt,
            //            Vector3.Up);
            //        effect.Projection = Matrix.CreatePerspectiveFieldOfView(
            //            MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
            //    }
            //    // Draw the mesh, using the effects set above.
            //    mesh.Draw();
            //}

            base.Draw(gameTime);

            var currentKeyboard = Keyboard.GetState();

            if (currentKeyboard.IsKeyDown(Keys.PrintScreen) && _previousKeyboard.IsKeyUp(Keys.PrintScreen))
            {
                var pp = GraphicsDevice.PresentationParameters;
                var data = new Color[pp.BackBufferWidth * pp.BackBufferHeight];
                GraphicsDevice.GetBackBufferData(data);

                // must be a less stupid way of doing this
                var texture = new Texture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight);
                texture.SetData<Color>(data);

                var filename = Path.Combine(Environment.CurrentDirectory, "screenshot.jpg");
                using (var stream = File.Create(filename))
                    texture.SaveAsJpeg(stream, texture.Width, texture.Height);
            }

            _previousKeyboard = currentKeyboard;
        }
 public void SaveJpg(Texture2D tex, string name)
 {
     FileStream f = new FileStream(name, FileMode.Create);
     tex.SaveAsJpeg(f, tex.Width, tex.Height);
     f.Close();
 }