예제 #1
0
        // Load a Direct2D bitmap from the given gdi resource.
        public D2D.Bitmap LoadBitmap(Bitmap drawingBitmap)
        {
            D2D.Bitmap result = null;

            //Lock the gdi resource
            BitmapData drawingBitmapData = drawingBitmap.LockBits(
                new Rectangle(0, 0, drawingBitmap.Width, drawingBitmap.Height),
                ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);

            //Prepare loading the image from gdi resource
            DataStream dataStream = new DataStream(
                drawingBitmapData.Scan0,
                drawingBitmapData.Stride * drawingBitmapData.Height,
                true, false);

            D2D.BitmapProperties properties = new D2D.BitmapProperties();
            properties.PixelFormat = new D2D.PixelFormat(
                DXGI.Format.B8G8R8A8_UNorm,
                D2D.AlphaMode.Premultiplied);

            //Load the image from the gdi resource
            result = new D2D.Bitmap(
                m_renderTarget,
                new Size(drawingBitmap.Width, drawingBitmap.Height),
                dataStream, drawingBitmapData.Stride,
                properties);

            //Unlock the gdi resource
            drawingBitmap.UnlockBits(drawingBitmapData);

            return(result);
        }
        /**
         * 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);
        }