Ejemplo n.º 1
0
        /// <summary>
        /// Draws the given bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="destinationRectangle">The target rectangle where to draw the bitmap.</param>
        /// <param name="opacity">The opacity.</param>
        /// <param name="interpolationMode">The interpolation mode.</param>
        /// <param name="frameIndex">The frame of the bitmap to be rendered.</param>
        public void DrawBitmap(
            BitmapResource bitmap,
            RectangleF destinationRectangle,
            float opacity = 1f,
            BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.NearestNeighbor,
            int frameIndex = 0)
        {
            if (m_renderTarget == null)
            {
                return;
            }

            bitmap.EnsureNotNull(nameof(bitmap));
            destinationRectangle.EnsureNotEmpty(nameof(destinationRectangle));
            opacity.EnsureInRange(0f, 1f, nameof(opacity));

            int bitmapFrameCount = bitmap.TotalFrameCount;

            frameIndex.EnsureInRange(0, bitmapFrameCount - 1, nameof(frameIndex));

            // Render the bitmap
            if (bitmapFrameCount > 1)
            {
                // Get the native bitmap object first
                // (if not, we may not have loaded it already and therefore
                //  missing size information)
                D2D.Bitmap nativeBitmap = bitmap.GetBitmap(m_device);

                // Calculate source rectangle
                int        framesX           = bitmap.FrameCountX;
                int        xFrameIndex       = frameIndex % framesX;
                int        yFrameIndex       = (frameIndex - xFrameIndex) / framesX;
                int        singleFrameWidth  = bitmap.SingleFramePixelWidth;
                int        singleFrameHeight = bitmap.SingleFramePixelHeight;
                RectangleF sourceRectangle   = new RectangleF(
                    xFrameIndex * singleFrameWidth,
                    yFrameIndex * singleFrameHeight,
                    singleFrameWidth, singleFrameHeight);

                // Render tiled bitmap
                m_renderTarget.DrawBitmap(
                    nativeBitmap,
                    destinationRectangle.ToDXRectangle(),
                    opacity,
                    (D2D.BitmapInterpolationMode)interpolationMode,
                    sourceRectangle.ToDXRectangle());
            }
            else
            {
                // Render non-tiled bitmap
                m_renderTarget.DrawBitmap(
                    bitmap.GetBitmap(m_device),
                    destinationRectangle.ToDXRectangle(),
                    opacity,
                    (D2D.BitmapInterpolationMode)interpolationMode);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Draws the given image.
        /// </summary>
        /// <param name="image">The source of pixel data to be rendered.</param>
        /// <param name="destinationOrigin">The origin point where to draw the image.</param>
        public void DrawImage(
            IImage image,
            Vector2 destinationOrigin)
        {
            if (m_renderTarget == null)
            {
                return;
            }

            image.EnsureNotNull(nameof(image));

            IImageInternal internalImage = image as IImageInternal;

            internalImage.EnsureNotNull(nameof(internalImage));

            if (m_deviceContext != null)
            {
                D2D.Image d2dImage = internalImage.GetImageObject(m_device) as D2D.Image;
                d2dImage.EnsureNotNull(nameof(d2dImage));

                m_deviceContext.DrawImage(
                    d2dImage,
                    destinationOrigin.ToDXVector(),
                    null,
                    D2D.InterpolationMode.Linear,
                    D2D.CompositeMode.SourceOver);
            }
            else
            {
                BitmapResource bitmap = internalImage.TryGetSourceBitmap();
                if (bitmap != null)
                {
                    this.DrawBitmap(bitmap, destinationOrigin);
                }
            }
        }