Windows Store 应用程序的 Bitmap.MakeTransparent? [英] Bitmap.MakeTransparent for Windows Store App?

查看:27
本文介绍了Windows Store 应用程序的 Bitmap.MakeTransparent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Winforms we have a Bitmap.MakeTransparent method to set a color to be transparent. Is there an equivalent method in UWP? (For WriteableBitmap or SoftwareBitmap etc.)

推荐答案

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

There are several ways to approach this:

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

First, if you are using the bitmap as a UI control's imagebrush, You can just set its Opacity per your requirement. A simple demo is like below:

<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.此处您可以找到有关使用 Direct2D 和 WIC Image 的示例一起.下面是两个简单的解决方案:

Second way is to use Windows Imaging Component. Here you can find sample about using Direct2D and WIC Image together. Below are two simple solutions:

1) 使用 IWICFormatConverter 接口.

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

In IWICFormatConverter.Initialize() set Alpha threshold per your requirement: The AlphaThreshold value is used to determine what level of opacity will be mapped to the fully transparent color in the converted format. A value of 9.8 implies that any pixel with an alpha value of less than 25 will be mapped to the transparent color. A value of 100 maps all pixels which are not fully opaque to the transparent color. Then you can draw bitmap by 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) 获取指定矩形的 IWICBitmapLockIWICBitmap,然后处理现在被 IWICBitmapLock 对象锁定的像素数据.

2) Obtain an IWICBitmapLock for a specified rectangle of the IWICBitmap, then process the pixel data that is now locked by the IWICBitmapLock object.

当然还有其他解决方案,比如使用WriteableBitmap直接操作图像像素.这是一个示例,可在 PixelDataProvider 类的帮助下访问像素访问位图中的像素.

Of course there are other solutions, such as Using a WriteableBitmap to manipulate image pixels directly. Here is a sample to access pixels with the help of PixelDataProvider class to access pixels in bitmap.

然而,除了 XAML UI 元素,Bitmap.MakeTransparent 还没有简单的替代品.所以这完全取决于您的使用模式.

However, except the XAML UI element, there is yet no simple replacement for Bitmap.MakeTransparent. So it all depends on your usage model.

这篇关于Windows Store 应用程序的 Bitmap.MakeTransparent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆