/// <summary> /// Gets the starting <see cref="Point"/> where the overlay needs to be drawn /// </summary> /// <param name="overlayPosition">The <see cref="OverlayPositionKind"/></param> /// <param name="targetImage">The <see cref="Image"/> that the overlay is added to</param> /// <param name="overlayBitMapImage">The overlay<see cref="Image"/></param> /// <returns>Starting <see cref="Point"/> where the overlay needs to be drawn</returns> private static Point GetOverlayPoint(OverlayPositionKind overlayPosition, Image targetImage, Image overlayBitMapImage) { var rightXpos = targetImage.Width - overlayBitMapImage.Width; var bottomYpos = targetImage.Height - overlayBitMapImage.Height; switch (overlayPosition) { case OverlayPositionKind.BottomRight: return(new Point(rightXpos, bottomYpos)); case OverlayPositionKind.TopRight: return(new Point(rightXpos, 0)); default: return(new Point(0, 0)); } }
/// <summary> /// The bitmap source with the image and the overlay. /// </summary> /// <param name="iconUri"> /// The uri to the source image. /// </param> /// <param name="overlayUri"> /// The uri of the overlay /// </param> /// <param name="overlayPosition"> /// The overlay position /// </param> /// <returns> /// The <see cref="BitmapSource"/>. /// </returns> public static BitmapSource WithOverlay(Uri iconUri, Uri overlayUri, OverlayPositionKind overlayPosition = OverlayPositionKind.TopLeft) { var source = new BitmapImage(iconUri); var overlay = new BitmapImage(overlayUri); var overlayBitMapImage = BitmapImage2Bitmap(overlay, (int)source.Width / 2, (int)source.Height / 2); var thingBitMapImage = BitmapImage2Bitmap(source); var img = new Bitmap(thingBitMapImage.Width, thingBitMapImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var xOverlay = overlayPosition == OverlayPositionKind.TopLeft || overlayPosition == OverlayPositionKind.BottomLeft ? 0 : (int)source.Width / 2; var yOverlay = overlayPosition == OverlayPositionKind.TopLeft || overlayPosition == OverlayPositionKind.TopRight ? 0 : (int)source.Height / 2; using (var gr = Graphics.FromImage(img)) { gr.CompositingMode = CompositingMode.SourceOver; gr.DrawImage(thingBitMapImage, new Point(0, 0)); gr.DrawImage(overlayBitMapImage, new Point(xOverlay, yOverlay)); } return(Bitmap2BitmapSource(img)); }
/// <summary> /// The bitmap source with the image and the overlay. /// </summary> /// <param name="iconUri"> /// The uri to the source image. /// </param> /// <param name="overlayUri"> /// The uri of the overlay /// </param> /// <param name="overlayPosition"> /// The overlay position /// </param> /// <returns> /// The <see cref="BitmapSource"/>. /// </returns> public static BitmapSource WithOverlay(Uri iconUri, Uri overlayUri, OverlayPositionKind overlayPosition = OverlayPositionKind.TopRight) { var source = new BitmapImage(iconUri); var overlay = new BitmapImage(overlayUri); var thingBitMapImage = BitmapImage2Bitmap(source); var overlayBitMapImage = BitmapImage2Bitmap(overlay, (int)Math.Floor(thingBitMapImage.Width * 0.75D), (int)Math.Floor(thingBitMapImage.Height * 0.75D)); var img = new Bitmap( (int)Math.Floor(thingBitMapImage.Width * 1.4D), (int)Math.Floor(thingBitMapImage.Height * 1.0D), System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (var gr = Graphics.FromImage(img)) { gr.CompositingMode = CompositingMode.SourceOver; gr.DrawImage(thingBitMapImage, GetMainImagePoint(img, thingBitMapImage)); gr.DrawImage(overlayBitMapImage, GetOverlayPoint(overlayPosition, img, overlayBitMapImage)); } return(Bitmap2BitmapSource(img)); }
/// <summary> /// Queries the <see cref="BitmapSource"/> with an icon overlayed from the cache if the cache contains it else the <see cref="BitmapSource"/> /// is created, added to the cache and then returned. /// </summary> /// <param name="uri"> /// The uri of the <see cref="BitmapSource"/> /// </param> /// <param name="overlayUri"> /// The uri of the overlay <see cref="BitmapSource"/> /// </param> /// <returns> /// An instance of <see cref="BitmapSource"/> /// </returns> public BitmapSource QueryOverlayBitmapSource(Uri uri, Uri overlayUri, OverlayPositionKind overlayPosition = OverlayPositionKind.TopLeft) { return(IconUtilities.WithOverlay(uri, overlayUri, overlayPosition)); }