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


C# Bitmap.CopyPixelsFromBuffer方法代码示例

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


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

示例1: Draw


//.........这里部分代码省略.........
                DateTime end = DateTime.Now;
                TimeSpan timeTaken = end - _firstDrawTime;

                var first = _first60fps;
                var second = 60 / timeTaken.TotalSeconds;
            }

            GraphicsDevice.Clear(Color.CornflowerBlue);

            Texture2D texture = (_mode % 2 == 0) ? _checkers60 : _checkers64;

            //for (int i = 0; i < 10; i++)
            {
                if (_mode < 2) //Without matrix
                    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearWrap, null, null);
                else //With matrix to rotate whole screen 180
                    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearWrap, null, null, null, Matrix.CreateRotationZ(MathHelper.ToRadians(180)) * Matrix.CreateTranslation(480, 320, 0));

                var size = new Vector2(texture.Width, texture.Height);

                //Plain
                spriteBatch.Draw(texture, new Vector2(5, 5), new Rectangle(0, 0, (int)size.X, (int)size.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //2x across
                spriteBatch.Draw(texture, new Vector2(5, 10 + size.Y), new Rectangle(0, 0, (int)size.X * 2, (int)size.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //2x down
                spriteBatch.Draw(texture, new Vector2(5, 15 + 2 *size.Y), new Rectangle(0, 0, (int)size.X, (int)size.Y * 2), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //2x2 down
                spriteBatch.Draw(texture, new Vector2(10 + size.X, 15 + 2 * size.Y), new Rectangle(0, 0, (int)size.X * 2, (int)size.Y * 2), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //1.5 across
                spriteBatch.Draw(texture, new Vector2(10 + size.X, 5), new Rectangle(0, 0, (int)(size.X * 1.5f), (int)size.Y), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //1.5 down
                spriteBatch.Draw(texture, new Vector2(15 + 2.5f * size.X, 5), new Rectangle(0, 0, (int)size.X, (int)(size.Y * 1.5f)), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                //1.5x1.5
                spriteBatch.Draw(texture, new Vector2(20 + 3.5f * size.X, 5), new Rectangle(0, 0, (int)(size.X * 1.5f), (int)(size.Y * 1.5f)), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);

                spriteBatch.End();
            }
            // TODO: Add your drawing code here

            base.Draw(gameTime);

            byte[] screenData = new byte[GraphicsDevice.Viewport.Width * GraphicsDevice.Viewport.Height * 4];
            GraphicsDevice.GetBackBufferData(screenData);
            //This returns the screen in BGRA format in XNA

            #if WINDOWS
            #if !(WINDOWS && DIRECTX)
            //In XNA and MonoGame.OpenGL we need to swap the R and B bytes so we are in RGBA
            for (var i = 0; i < screenData.Length; i += 4)
            {
                byte temp = screenData[i];
                screenData[i] = screenData[i + 2];
                screenData[i + 2] = temp;
            }
            #endif

            using (var bitmap = new Bitmap(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height))
            {
                var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); //This is actually RGBA byte format

                Marshal.Copy(screenData, 0, data.Scan0, screenData.Length);

                bitmap.UnlockBits(data);

                bitmap.Save("out.png", ImageFormat.Png);
            }
            #endif

            #if IOS
            using (var colorSpace = CGColorSpace.CreateDeviceRGB())
            using (var provider = new CGDataProvider(screenData, 0, screenData.Length))
            using (var cgImage = new CGImage(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 8, 4 * 8, 4 * GraphicsDevice.Viewport.Width, colorSpace, CGBitmapFlags.ByteOrderDefault, provider, null, false, CGColorRenderingIntent.Default))
            using (var image = UIImage.FromImage(cgImage))
            {
                image.SaveToPhotosAlbum(null);
            }
            #endif

            #if ANDROID
            using (var buffer = ByteBuffer.Wrap(screenData))
            using (var bitmap = Bitmap.CreateBitmap(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, Bitmap.Config.Argb8888))
            {
                bitmap.CopyPixelsFromBuffer(buffer);

                var filename = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "out.png");

                //Make sure you have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
                using (var output = File.OpenWrite(filename))
                {
                    bitmap.Compress(Bitmap.CompressFormat.Png, 100, output);
                }
            }
            #endif
        }
开发者ID:danzel,项目名称:WrapTest,代码行数:101,代码来源:Game1.cs


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