【问题标题】:Bitmap.MakeTransparent for Windows Store App?Windows Store 应用程序的 Bitmap.MakeTransparent?
【发布时间】:2016-03-19 01:09:11
【问题描述】:

在 Winforms 中,我们有一个 Bitmap.MakeTransparent 方法来将颜色设置为透明。 UWP中有等效的方法吗? (对于WriteableBitmapSoftwareBitmap 等)

【问题讨论】:

标签: c# windows-store-apps win-universal-app uwp


【解决方案1】:

有几种方法可以解决这个问题:

首先,如果您将位图用作 UI 控件的图像画笔,您可以根据需要设置其不透明度。一个简单的演示如下:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Rectangle x:Name="rct" Width="800" Height="800">
      <Rectangle.Fill>
       <ImageBrush x:Name="ib" ImageSource="Assets//hero.bmp" Stretch="Uniform" Opacity="0.5"/>
      </Rectangle.Fill>
   </Rectangle>
</Grid>

第二种方法是使用Windows Imaging Component。 Here 您可以找到有关同时使用 Direct2D 和 WIC Image 的示例。以下是两个简单的解决方案:

1) 使用IWICFormatConverter 接口。

在 IWICFormatConverter.Initialize() 中,根据您的要求设置 Alpha 阈值:AlphaThreshold 值用于确定将什么级别的不透明度映射到转换格式中的完全透明颜色。值 9.8 意味着任何 alpha 值小于 25 的像素都将映射到透明颜色。值 100 将所有不完全不透明的像素映射到透明颜色。然后您可以通过 D2D 绘制位图。

 hr = m_pConvertedSourceBitmap->Initialize(
           pFrame,                          // Input bitmap to convert
           GUID_WICPixelFormat32bppPBGRA,   // Destination pixel format
           WICBitmapDitherTypeNone,         // Specified dither pattern
           NULL,                            // Specify a particular palette 
           0.f,                             // Alpha threshold
           WICBitmapPaletteTypeCustom       // Palette translation type
           );

2) 获取IWICBitmap的指定矩形的IWICBitmapLock,然后处理现在被IWICBitmapLock对象锁定的像素数据。

当然还有其他解决方案,比如使用 WriteableBitmap 直接操作图像像素。这里有一个sample,可以借助 PixelDataProvider 类来访问像素,以访问位图中的像素。

但是,除了 XAML UI 元素之外,还没有简单的替代 Bitmap.MakeTransparent 的方法。所以这一切都取决于你的使用模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多