/// <summary> /// Creates a ScaledBitmap from the given original bitmap and replace it's transparent pixels with the /// given new color. /// </summary> /// <param name="originalBmp">The original bitmap.</param> /// <param name="transparentColor">The selected transparent color on the original bitmap.</param> /// <param name="replaceTo"> /// The transparent pixels on the original bitmap will be replaced with this color. /// </param> /// <returns>The created ScaledBitmap.</returns> /// <remarks>Disposing originalBmp is the responsibility of the caller.</remarks> public static ScaledBitmap FromBitmap(Bitmap originalBmp, Color transparentColor, Color replaceTo) { if (null == originalBmp) { throw new ArgumentNullException("originalBmp"); } if (PixelFormat.Format24bppRgb != originalBmp.PixelFormat) { throw new ArgumentException("Pixel format of the given Bitmap must be PixelFormat.Format24bppRgb", "originalBmp"); /// TODO: implement other pixel format support (?) } ScaledBitmap original = ScaledBitmap.FromBitmap(originalBmp); ScaledBitmap retBmp = new ScaledBitmap(original.Width, original.Height); BitmapAccess retBmpAccess = BitmapAccess.FromBitmap(retBmp); retBmpAccess.Clear(replaceTo); original.MakeTransparent(transparentColor); retBmpAccess.DrawBitmap(original, 0, 0); retBmpAccess.Dispose(); original.Dispose(); return(retBmp); }
/// <summary> /// Creates a ScaledBitmap from the given original bitmap and replace it's transparent pixels with the /// pixels of another bitmap. /// </summary> /// <param name="originalBmp">The original bitmap.</param> /// <param name="transparentColor">The selected transparent color on the original bitmap.</param> /// <param name="replaceTo"> /// The transparent pixels on the original bitmap will be replaced with the pixels of this bitmap. /// </param> /// <returns>The created ScaledBitmap.</returns> /// <remarks>Disposing originalBmp and replaceTo is the responsibility of the caller.</remarks> public static ScaledBitmap FromBitmap(Bitmap originalBmp, Color transparentColor, Bitmap replaceTo) { if (null == originalBmp) { throw new ArgumentNullException("originalBmp"); } if (null == replaceTo) { throw new ArgumentNullException("replaceTo"); } if (PixelFormat.Format24bppRgb != originalBmp.PixelFormat || PixelFormat.Format24bppRgb != replaceTo.PixelFormat) { throw new ArgumentException("Pixel format of the given Bitmaps must be PixelFormat.Format24bppRgb"); /// TODO: implement other pixel format support (?) } ScaledBitmap original = ScaledBitmap.FromBitmap(originalBmp); ScaledBitmap replaceToBmp = ScaledBitmap.FromBitmap(replaceTo); ScaledBitmap retBmp = new ScaledBitmap(original.Width, original.Height); BitmapAccess retBmpAccess = BitmapAccess.FromBitmap(retBmp); for (int currX = 0; currX < retBmp.Width; currX = currX + replaceTo.Width) { for (int currY = 0; currY < retBmp.Height; currY = currY + replaceTo.Height) { retBmpAccess.DrawBitmap(replaceToBmp, currX, currY); } } original.MakeTransparent(transparentColor); retBmpAccess.DrawBitmap(original, 0, 0); retBmpAccess.Dispose(); original.Dispose(); replaceToBmp.Dispose(); return(retBmp); }