Exemplo n.º 1
0
 public void DisposeResources(GraphicsContext graphics)
 {
     _updateTimer?.Change(Timeout.Infinite, Timeout.Infinite);
     _updateTimer = null;
     _bitmap?.Dispose();
     _bitmap = null;
     _brush?.Dispose();
     _bitmapMemory = null;
 }
Exemplo n.º 2
0
        private void RenderBitmap(GraphicsContext graphics)
        {
            Log.InfoFormat("Rendering Bitmap");

            int   xOffset = 0, zOffset = 0;
            Size2 size = Size2.Empty;

            ChunkCacheBitmap[] cache;

            lock (_renderSync)
            {
                _isDirty = false;
                xOffset  = Math.Min(0, _minChunk.X) * -1;
                zOffset  = Math.Min(0, _minChunk.Z) * -1;
                size     = _imageSize;
                cache    = _chunkCache.Values.ToArray();
            }


            var arrSize = size.Width * size.Height * 4;

            if (_bitmap == null || _bitmapMemory == null || _bitmapMemory.Length != arrSize)
            {
                _bitmapMemory = new byte[arrSize];
                _bitmap       = new SharpDX.Direct2D1.Bitmap(graphics.Target2D, size, new BitmapProperties(graphics.Target2D.PixelFormat));
            }
            _scale = Math.Min(graphics.ViewportSize.Width / (float)size.Width,
                              graphics.ViewportSize.Height / (float)size.Height);

            _scaleMatrix = Matrix3x2.Scaling(_scale);

            if (cache.Length == 0)
            {
                return;
            }

            foreach (var chunk in cache)
            {
                for (int z = 0; z < 16; z++)
                {
                    Array.Copy(chunk.BitmapMemory, z * 16 * 4, _bitmapMemory, (size.Width * ((chunk.Z + zOffset) * 16 + z) * 4) + ((chunk.X + xOffset) * 16) * 4, 16 * 4);
                }
                //chunk.BitmapMemory.CopyTo(_bitmapMemory, (int)((_imageSize.Width * ((chunk.Z + _zOffset) * 16)) + ((chunk.X + _xOffset)* 16))*4);
            }

            _bitmap.CopyFromMemory(_bitmapMemory, size.Width * 4);
            Log.InfoFormat("Rendering Bitmap Complete | Size: {0}", _bitmapMemory.Length);
        }
            public SharpDX.Direct2D1.Bitmap LoadFromFile(RenderTarget renderTarget, System.Drawing.Bitmap bitmap)
            {
                SharpDX.Direct2D1.Bitmap match;

                if (_cache.TryGetValue(bitmap, out match))
                {
                    return(match);
                }

                var sourceArea       = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
                var size             = new Size2(bitmap.Width, bitmap.Height);

                // Transform pixels from BGRA to RGBA
                int stride = bitmap.Width * sizeof(int);

                using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
                {
                    // Lock System.Drawing.Bitmap
                    var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                    // Convert all pixels
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            // Not optimized
                            byte B    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte G    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte R    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte A    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            int  rgba = R | (G << 8) | (B << 16) | (A << 24);
                            tempStream.Write(rgba);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;

                    match = new SharpDX.Direct2D1.Bitmap(renderTarget, size, tempStream, stride, bitmapProperties);

                    _cache.TryAdd(bitmap, match);
                }
                return(match);
            }