Example #1
0
        private void CreateBitmap()
        {
            BitmapData bitmapData = this.orgBitmap.LockBits(new Rectangle(0, 0, this.orgBitmap.Width, this.orgBitmap.Height),
                                                            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            using (DataStream dataStream = new DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false))
            {
                PixelFormat      format     = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
                BitmapProperties properties = new BitmapProperties();
                properties.HorizontalDpi = properties.VerticalDpi = 96;
                properties.PixelFormat   = format;
                if (this.SpriteBitmap != null && !this.SpriteBitmap.Disposed)
                {
                    this.SpriteBitmap.Dispose();
                }
                this.SpriteBitmap = new SlimDX.Direct2D.Bitmap(this.batch.DWRenderTarget, new Size(this.orgBitmap.Width, this.orgBitmap.Height),
                                                               dataStream, bitmapData.Stride, properties);
                this.orgBitmap.UnlockBits(bitmapData);
            }
        }
        /**
         * Render with or without texture
         **/
        protected override void _render()
        {
            if (this._bitmap == null)
            {
                // Render in solid color
                if (_brush == null)
                    _brush = new SolidColorBrush(Device.RenderTarget, Color);

                Device.RenderTarget.FillRectangle(_brush, _rectangle);

                return;
            }

            if (this._d2dBitmap == null)
            {
                // Load the texture
                var bitmapData = this._bitmap.LockBits(
                    new Rectangle(new Point(0, 0), this._bitmap.Size),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format32bppPArgb
                );
                var dataStream = new SlimDX.DataStream(
                    bitmapData.Scan0,
                    bitmapData.Stride * bitmapData.Height,
                    true,
                    false
                );
                var d2dPixelFormat = new SlimDX.Direct2D.PixelFormat(
                    SlimDX.DXGI.Format.B8G8R8A8_UNorm,
                    SlimDX.Direct2D.AlphaMode.Premultiplied
                );
                var d2dBitmapProperties = new SlimDX.Direct2D.BitmapProperties();
                d2dBitmapProperties.PixelFormat = d2dPixelFormat;

                _d2dBitmap = new SlimDX.Direct2D.Bitmap(
                    Device.RenderTarget,
                    new Size(this._bitmap.Width, this._bitmap.Height),
                    dataStream,
                    bitmapData.Stride,
                    d2dBitmapProperties
                );
                this._bitmap.UnlockBits(bitmapData);
            }

            // Render the texture
            Device.RenderTarget.DrawBitmap(_d2dBitmap, _rectangle);
        }