private static CachedBitmapInfo GetOrCreateAlphaMask(GlowBitmapPart bitmapPart) { if (_transparencyMasks[(int)bitmapPart] == null) { var bitmapImage = new BitmapImage(new Uri($"pack://application:,,,/HandyControl;Component/Resources/Images/GlowWindow/{bitmapPart}.png")); var array = new byte[BytesPerPixelBgra32 * bitmapImage.PixelWidth * bitmapImage.PixelHeight]; var stride = BytesPerPixelBgra32 * bitmapImage.PixelWidth; bitmapImage.CopyPixels(array, stride, 0); _transparencyMasks[(int)bitmapPart] = new CachedBitmapInfo( array, bitmapImage.PixelWidth, bitmapImage.PixelHeight); } return(_transparencyMasks[(int)bitmapPart]); }
/// <summary> /// 工厂方法:用设定的颜色和位置创建一个 GlowBitmap 对象。 /// </summary> /// <param name="drawingContext"></param> /// <param name="bitmapPart"></param> /// <param name="color"></param> /// <returns></returns> public static GlowEdgeBitmap Create(GlowEdgeDrawingContext dc, GlowEdgeBitmapParts part, Color color) { CachedBitmapInfo mast = GetOrCreateAlphaMast(part); GlowEdgeBitmap glowEdgeBitmap = new GlowEdgeBitmap(dc.ScreenDC, mast.Width, mast.Height); // 创建数据 for (int i = 0; i < mast.DIBits.Length; i += BytesPerPixelBgra32) { byte a = mast.DIBits[i + 3]; byte r = PremultiplyAlpha(color.R, a); byte g = PremultiplyAlpha(color.G, a); byte b = PremultiplyAlpha(color.B, a); Marshal.WriteByte(glowEdgeBitmap.DIBits, i, b); Marshal.WriteByte(glowEdgeBitmap.DIBits, i + 1, g); Marshal.WriteByte(glowEdgeBitmap.DIBits, i + 2, r); Marshal.WriteByte(glowEdgeBitmap.DIBits, i + 3, a); } return(glowEdgeBitmap); }
/// <summary> /// 读取图片的 Alpha 通道作为阴影的 Alpha 通道 /// </summary> /// <param name="part"></param> /// <returns></returns> private static CachedBitmapInfo GetOrCreateAlphaMast(GlowEdgeBitmapParts part) { int index = (int)part; if (_transparencyMasks[index] == null) { BitmapImage mastImage = new BitmapImage( new Uri($"pack://application:,,,/WindowCustomizeTest;Component/GlowWindowCore/Resource/{part}.png" /*, UriKind.Relative*/)); byte[] pixels = new byte[BytesPerPixelBgra32 * mastImage.PixelWidth * mastImage.PixelHeight]; int stride = BytesPerPixelBgra32 * mastImage.PixelWidth; mastImage.CopyPixels(pixels, stride, 0); mastImage.Freeze(); _transparencyMasks[index] = new CachedBitmapInfo(pixels, mastImage.PixelWidth, mastImage.PixelHeight); } return(_transparencyMasks[index]); }
private static void CreateAlphaMask() { // run once if (alphaMasks[0] != null) { return; } string assembly = typeof(GlowBitmap).Assembly.GetName().Name; for (int i = 0; i < 12; i++) { string path = String.Format("Images/{0}.png", (BitmapPart)i); var uri = new Uri(String.Format("pack://application:,,,/{0};component/{1}", assembly, path), UriKind.Absolute); var bitmapImage = new BitmapImage(uri); byte[] array = new byte[4 * bitmapImage.PixelWidth * bitmapImage.PixelHeight]; int stride = 4 * bitmapImage.PixelWidth; bitmapImage.CopyPixels(array, stride, 0); alphaMasks[i] = new CachedBitmapInfo(array, bitmapImage.PixelWidth, bitmapImage.PixelHeight); } }
} // ctor public static GlowBitmap FromImage(int imagePos, Color color) { CreateAlphaMask(); CachedBitmapInfo alphaMask = alphaMasks[imagePos]; IntPtr hdc = NativeMethods.GetDC(IntPtr.Zero); var glowBitmap = new GlowBitmap(hdc, alphaMask.Width, alphaMask.Height); for (int i = 0; i < alphaMask.DIBits.Length; i += 4) { byte alpha = alphaMask.DIBits[i + 3]; byte red = (byte)((double)(color.R * alpha) / 255.0); byte green = (byte)((double)(color.G * alpha) / 255.0); byte blue = (byte)((double)(color.B * alpha) / 255.0); Marshal.WriteByte(glowBitmap.DIBits, i, blue); Marshal.WriteByte(glowBitmap.DIBits, i + 1, green); Marshal.WriteByte(glowBitmap.DIBits, i + 2, red); Marshal.WriteByte(glowBitmap.DIBits, i + 3, alpha); } NativeMethods.ReleaseDC(IntPtr.Zero, hdc); return(glowBitmap); } // ctor static