こんにちは。
Windows10でUWPのアプリケーションを開発しています。
Visual Studio 2017 Communityを使っています。
###前提・実現したいこと
Imageを2枚重ねて、奥に地のイメージ、
手前のImageにPNG(ほぼモノクロ)の背景部分(地の部分)を透明にして、表示したいです。
単純に重ねると、奥の画像は見えないのです。
どのようなことをすればよいでしょう。
image background="transparent"みたいな指定はありませんか?
###該当のソースコード
xaml
1 <Grid > 2 3 <Image x:Name="Background" 4 Source="ms-appx:///Assets/background.png" > 5 </Image> 6 <Image x:Name="mainphoto" /> 7</Grid>
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586
です。
よろしくお願いします。
Opacity="0.5"だと手前のイメージ全体が薄くなってしまいます。
手前のイメージの背景部分のみをうすくできればと思っています。
C#
1using Windows.Graphics.Imaging; 2using Windows.UI; 3using Windows.UI.Xaml.Media.Imaging; 4 5namespace TESTImage 6{ 7 class TransparentImage 8 { 9 private BitmapSource GetPngWithTransparentBackground(BitmapSource source, Color transparentColor) 10 { 11 BitmapFrame frame = BitmapFrame.Create(source); 12 var writeableBitmap = new WriteableBitmap(frame); 13 14 if (writeableBitmap.Format.BitsPerPixel != 32) 15 { 16 return null; 17 } 18 19 var rect = new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight); 20 var bytesPerPixel = (writeableBitmap.Format.BitsPerPixel + 7) / 8; // 1 ピクセル当たりのバイト数(4 になるはず) 21 var stride = writeableBitmap.PixelWidth * bytesPerPixel; // 幅方向のバイト数 22 var arraySize = stride * writeableBitmap.PixelHeight; 23 24 var sourcePixels = new byte[arraySize]; 25 var newPixels = new byte[arraySize]; 26 27 writeableBitmap.CopyPixels(sourcePixels, stride, 0); 28 writeableBitmap.CopyPixels(newPixels, stride, 0); 29 30 byte a, r, g, b; 31 int index; // 左上隅からのバイトのインデックス 32 33 for (int y = 0; y < writeableBitmap.PixelHeight; y++) 34 { 35 for (int x = 0; x < writeableBitmap.PixelWidth; x++) 36 { 37 index = x * bytesPerPixel + y * stride; 38 b = sourcePixels[index]; 39 g = sourcePixels[index + 1]; 40 r = sourcePixels[index + 2]; 41 a = sourcePixels[index + 3]; 42 43 Color color = Color.FromArgb(a, r, g, b); 44 45 if (color.Equals(transparentColor)) 46 { 47 newPixels[index + 3] = 0; // アルファ値を持つバイトを 0 に設定する 48 } 49 } 50 } 51 52 writeableBitmap.WritePixels(rect, newPixels, stride, 0); 53 54 return writeableBitmap; 55 } 56 57 //上記のメソッドを利用するコードは以下のとおりです。imageはPNG画像。Colors.White のピクセルを透明化する。 58 private WriteableBitmap imagetransparent(WriteableBitmap image) 59 { 60 return this.GetPngWithTransparentBackground(image, Colors.White); 61 } 62 } 63}
では動作しませんでした。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。