/// <summary> /// Computes the clipping rectangle of this render context in screen coordinates. /// </summary> /// <returns>The clipping rectangle of this render context in screen coordinates.</returns> private RCIntRectangle ComputeAbsClipRectangle() { RCIntRectangle absClipRect = this.targetObject.AbsoluteRange; UIObject current = this.targetObject.Parent; while (current != null && absClipRect != RCIntRectangle.Undefined) { absClipRect.Intersect(current.AbsoluteClip != RCIntRectangle.Undefined ? current.AbsoluteClip : current.AbsoluteRange); current = current.Parent; } return(absClipRect); }
/// <summary> /// Internal method to render a sprite in order to clip it with the clip rectangle. /// </summary> /// <param name="sprite">The sprite to render.</param> /// <param name="position">The position where to render in screen coordinates.</param> /// <param name="absSection"> /// The section of the sprite to render in the coordinate-system of the XNA-texture. /// </param> private void RenderSpriteWithClip(XnaSprite sprite, RCIntVector position, RCIntRectangle absSection) { /// Compute the clipped section in the coordinate-system of the XNA-texture. RCIntRectangle clippedSection = new RCIntRectangle(this.Clip.Location - position + absSection.Location, this.Clip.Size); clippedSection.Intersect(absSection); if (clippedSection != RCIntRectangle.Undefined) { Microsoft.Xna.Framework.Rectangle srcRect = new Microsoft.Xna.Framework.Rectangle(clippedSection.X, clippedSection.Y, clippedSection.Width, clippedSection.Height); this.implementation.SpriteBatch.Draw(sprite.XnaTexture, new Vector2((float)position.X + (float)clippedSection.X - (float)absSection.X, (float)position.Y + (float)clippedSection.Y - (float)absSection.Y), srcRect, Microsoft.Xna.Framework.Color.White); } }
/// <summary> /// Copies a section of scaled pixels from the source bitmap to the scaled bitmaps of the target bitmap /// to a given position. /// </summary> /// <param name="source">The source bitmap.</param> /// <param name="target">The target bitmap.</param> /// <param name="srcPixelSize">The pixel size of the source bitmap.</param> /// <param name="tgtPixelSize">The pixel size of the target bitmap.</param> /// <param name="srcSection">The section of the source bitmap to copy.</param> /// <param name="tgtPosition">The position in the target bitmap to copy to.</param> public static void CopyBitmapScaled(Bitmap source, Bitmap target, RCIntVector srcPixelSize, RCIntVector tgtPixelSize, RCIntRectangle srcSection, RCIntVector tgtPosition) { /// Check the parameters. if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } if (srcPixelSize == RCIntVector.Undefined) { throw new ArgumentNullException("srcPixelSize"); } if (tgtPixelSize == RCIntVector.Undefined) { throw new ArgumentNullException("tgtPixelSize"); } if (srcSection == RCIntRectangle.Undefined) { throw new ArgumentNullException("srcSection"); } if (tgtPosition == RCIntVector.Undefined) { throw new ArgumentNullException("tgtPosition"); } if (PixelFormat.Format24bppRgb != source.PixelFormat || PixelFormat.Format24bppRgb != target.PixelFormat) { throw new ArgumentException("Pixel format of the given Bitmaps must be PixelFormat.Format24bppRgb"); } /// Compute the source and the target sections. srcSection.Intersect(new RCIntRectangle(0, 0, source.Width / srcPixelSize.X, source.Height / srcPixelSize.Y)); if (srcSection == RCIntRectangle.Undefined) { return; } RCIntRectangle tgtSection = new RCIntRectangle(tgtPosition.X, tgtPosition.Y, srcSection.Width, srcSection.Height); tgtSection.Intersect(new RCIntRectangle(0, 0, target.Width / tgtPixelSize.X, target.Height / tgtPixelSize.Y)); if (tgtSection == RCIntRectangle.Undefined) { return; } /// Lock the bytes of the bitmaps for copying. BitmapData srcRawData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData tgtRawData = target.LockBits(new Rectangle(0, 0, target.Width, target.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); /// Copy the pixels. for (int row = 0; row < srcSection.Height; row++) { if (row < tgtSection.Height) { for (int col = 0; col < srcSection.Width; col++) { if (col < tgtSection.Width) { CopyPixelScaled(srcRawData, tgtRawData, new RCIntVector(srcSection.X + col, srcSection.Y + row), new RCIntVector(tgtSection.X + col, tgtSection.Y + row), srcPixelSize, tgtPixelSize); } } } } source.UnlockBits(srcRawData); target.UnlockBits(tgtRawData); }