/// <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]); }