質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/29 12:42

投稿

TN8001
TN8001

スコア10180

answer CHANGED
@@ -1,183 +1,183 @@
1
- 普通に考えるとこの辺を使って、アルファを反転させた画像を作るんでしょうね。
2
- [BitmapSource.CopyPixels メソッド (System.Windows.Media.Imaging) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.imaging.bitmapsource.copypixels?view=net-6.0)
3
-
4
- [WPFにおける画像のピクセル値への高速アクセス - Qiita](https://qiita.com/MaruiMisato/items/b350979f35bc0e9e0f51)
5
-
6
- わたしはこういった(めんどくさそうな)ことは嫌いなので、調べている途中で見たこれでやってみました。
7
- [wpf - How to Invert Color of XAML PNG Images using C#? - Stack Overflow](https://stackoverflow.com/questions/45093399/how-to-invert-color-of-xaml-png-images-using-c)
8
-
9
- 注)私はシェーダーについて何ひとつわかっていません。
10
- ```xaml
11
- <Window
12
- x:Class="Questions374313.MainWindow"
13
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
- xmlns:local="clr-namespace:Questions374313"
16
- Width="800"
17
- Height="450"
18
- Background="MistyRose">
19
- <Window.Resources>
20
-
21
- <!--<BitmapImage x:Key="original" UriSource="orange.png" />-->
22
- <!-- オレンジ画像を使う場合↑を生かして、↓をコメント化 -->
23
- <DrawingImage x:Key="original">
24
- <DrawingImage.Drawing>
25
- <DrawingGroup>
26
- <GeometryDrawing Brush="Transparent">
27
- <GeometryDrawing.Geometry>
28
- <RectangleGeometry Rect="0,0,300,277" />
29
- </GeometryDrawing.Geometry>
30
- </GeometryDrawing>
31
- <GeometryDrawing>
32
- <GeometryDrawing.Geometry>
33
- <GeometryGroup>
34
- <EllipseGeometry
35
- Center="150,138.5"
36
- RadiusX="140"
37
- RadiusY="100">
38
- <EllipseGeometry.Transform>
39
- <TransformGroup>
40
- <ScaleTransform CenterX="150" CenterY="138.5" />
41
- <SkewTransform CenterX="150" CenterY="138.5" />
42
- <RotateTransform Angle="140" CenterX="150" CenterY="138.5" />
43
- <TranslateTransform />
44
- </TransformGroup>
45
- </EllipseGeometry.Transform>
46
- </EllipseGeometry>
47
- </GeometryGroup>
48
- </GeometryDrawing.Geometry>
49
- <GeometryDrawing.Brush>
50
- <LinearGradientBrush>
51
- <GradientStop Offset="0.0" Color="#FFDD6200" />
52
- <GradientStop Offset="1.0" Color="#0000" />
53
- </LinearGradientBrush>
54
- </GeometryDrawing.Brush>
55
- <GeometryDrawing.Pen>
56
- <Pen Brush="#FFFFD250" Thickness="20" />
57
- </GeometryDrawing.Pen>
58
- </GeometryDrawing>
59
- </DrawingGroup>
60
- </DrawingImage.Drawing>
61
- </DrawingImage>
62
-
63
- <Image x:Key="invert" Source="{StaticResource original}">
64
- <Image.Effect>
65
- <local:InvertEffect />
66
- </Image.Effect>
67
- </Image>
68
- </Window.Resources>
69
-
70
- <UniformGrid
71
- HorizontalAlignment="Left"
72
- VerticalAlignment="Top"
73
- Rows="1">
74
- <UniformGrid.Resources>
75
- <Style TargetType="Rectangle">
76
- <Setter Property="Width" Value="300" />
77
- <Setter Property="Height" Value="277" />
78
- <Setter Property="Fill" Value="Blue" />
79
- </Style>
80
- </UniformGrid.Resources>
81
-
82
- <GroupBox Header="original">
83
- <Image Source="{StaticResource original}" />
84
- </GroupBox>
85
-
86
- <GroupBox Header="original - OpacityMask">
87
- <Viewbox>
88
- <Rectangle>
89
- <Rectangle.OpacityMask>
90
- <ImageBrush ImageSource="{StaticResource original}" />
91
- </Rectangle.OpacityMask>
92
- </Rectangle>
93
- </Viewbox>
94
- </GroupBox>
95
-
96
- <GroupBox Header="fake">
97
- <Viewbox>
98
- <Grid>
99
- <Rectangle />
100
- <Rectangle Fill="White">
101
- <Rectangle.OpacityMask>
102
- <ImageBrush ImageSource="{StaticResource original}" />
103
- </Rectangle.OpacityMask>
104
- </Rectangle>
105
- </Grid>
106
- </Viewbox>
107
- </GroupBox>
108
-
109
- <GroupBox Header="invert - OpacityMask">
110
- <Viewbox>
111
- <Rectangle>
112
- <Rectangle.OpacityMask>
113
- <VisualBrush Visual="{StaticResource invert}" />
114
- </Rectangle.OpacityMask>
115
- </Rectangle>
116
- </Viewbox>
117
- </GroupBox>
118
- </UniformGrid>
119
- </Window>
120
- ```
121
-
122
- ```C#
123
- using System;
124
- using System.IO;
125
- using System.Windows;
126
- using System.Windows.Media;
127
- using System.Windows.Media.Effects;
128
-
129
- namespace Questions374313
130
- {
131
- public partial class MainWindow : Window
132
- {
133
- public MainWindow() => InitializeComponent();
134
- }
135
-
136
-
137
- // [wpf - How to Invert Color of XAML PNG Images using C#? - Stack Overflow](https://stackoverflow.com/questions/45093399/how-to-invert-color-of-xaml-png-images-using-c)
138
- internal class InvertEffect : ShaderEffect
139
- {
140
- public Brush Input { get => (Brush)GetValue(InputProperty); set => SetValue(InputProperty, value); }
141
- public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(InvertEffect), 0);
142
-
143
- private const string _kshaderAsBase64 = @"AAP///7/HwBDVEFCHAAAAE8AAAAAA///AQAAABwAAAAAAQAASAAAADAAAAADAAAAAQACADgAAAAAAAAAaW5wdXQAq6sEAAwAAQABAAEAAAAAAAAAcHNfM18wAE1pY3Jvc29mdCAoUikgSExTTCBTaGFkZXIgQ29tcGlsZXIgMTAuMQCrUQAABQAAD6AAAIA/AAAAAAAAAAAAAAAAHwAAAgUAAIAAAAOQHwAAAgAAAJAACA+gQgAAAwAAD4AAAOSQAAjkoAIAAAMAAAiAAAD/gQAAAKAFAAADAAgHgAAA/4AAAOSAAQAAAgAICIAAAP+A//8AAA==";
144
-
145
- private static readonly PixelShader _shader;
146
-
147
- static InvertEffect()
148
- {
149
- _shader = new PixelShader();
150
- _shader.SetStreamSource(new MemoryStream(Convert.FromBase64String(_kshaderAsBase64)));
151
- }
152
-
153
- public InvertEffect()
154
- {
155
- PixelShader = _shader;
156
- UpdateShaderValue(InputProperty);
157
- }
158
- }
159
- }
160
- ```
161
-
162
- ```HLSL
163
- sampler2D input : register(s0);
164
-
165
- float4 main(float2 uv : TEXCOORD) : COLOR
166
- {
167
- float4 color = tex2D(input, uv);
168
- float alpha = 1 - color.a;
169
-
170
- // color = 1 - color;
171
- color.a = alpha;
172
- color.rgb *= alpha;
173
-
174
- return color;
175
- }
176
- ```
177
- ぶっちゃけシェーダーなら何でもありみたいなものでしょうから、こんな回りくどいことをしなくてももっと直接適用できるでしょうが^^;
178
- ![アプリ画像](d90addbe0f36f8336603b85e99a4bbe4.png)
179
-
180
- ---
181
-
182
- 途中まで調べていたので回答しましたが、「特に意味はない」と言われてしまうと萎えますね^^;
1
+ 普通に考えるとこの辺を使って、アルファを反転させた画像を作るんでしょうね。
2
+ [BitmapSource.CopyPixels メソッド (System.Windows.Media.Imaging) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.imaging.bitmapsource.copypixels?view=net-6.0)
3
+
4
+ [WPFにおける画像のピクセル値への高速アクセス - Qiita](https://qiita.com/MaruiMisato/items/b350979f35bc0e9e0f51)
5
+
6
+ わたしはこういった(めんどくさそうな)ことは嫌いなので、調べている途中で見たこれでやってみました。
7
+ [wpf - How to Invert Color of XAML PNG Images using C#? - Stack Overflow](https://stackoverflow.com/questions/45093399/how-to-invert-color-of-xaml-png-images-using-c)
8
+
9
+ 注)私はシェーダーについて何ひとつわかっていません。
10
+ ```xml
11
+ <Window
12
+ x:Class="Questions374313.MainWindow"
13
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
15
+ xmlns:local="clr-namespace:Questions374313"
16
+ Width="800"
17
+ Height="450"
18
+ Background="MistyRose">
19
+ <Window.Resources>
20
+
21
+ <!--<BitmapImage x:Key="original" UriSource="orange.png" />-->
22
+ <!-- オレンジ画像を使う場合↑を生かして、↓をコメント化 -->
23
+ <DrawingImage x:Key="original">
24
+ <DrawingImage.Drawing>
25
+ <DrawingGroup>
26
+ <GeometryDrawing Brush="Transparent">
27
+ <GeometryDrawing.Geometry>
28
+ <RectangleGeometry Rect="0,0,300,277" />
29
+ </GeometryDrawing.Geometry>
30
+ </GeometryDrawing>
31
+ <GeometryDrawing>
32
+ <GeometryDrawing.Geometry>
33
+ <GeometryGroup>
34
+ <EllipseGeometry
35
+ Center="150,138.5"
36
+ RadiusX="140"
37
+ RadiusY="100">
38
+ <EllipseGeometry.Transform>
39
+ <TransformGroup>
40
+ <ScaleTransform CenterX="150" CenterY="138.5" />
41
+ <SkewTransform CenterX="150" CenterY="138.5" />
42
+ <RotateTransform Angle="140" CenterX="150" CenterY="138.5" />
43
+ <TranslateTransform />
44
+ </TransformGroup>
45
+ </EllipseGeometry.Transform>
46
+ </EllipseGeometry>
47
+ </GeometryGroup>
48
+ </GeometryDrawing.Geometry>
49
+ <GeometryDrawing.Brush>
50
+ <LinearGradientBrush>
51
+ <GradientStop Offset="0.0" Color="#FFDD6200" />
52
+ <GradientStop Offset="1.0" Color="#0000" />
53
+ </LinearGradientBrush>
54
+ </GeometryDrawing.Brush>
55
+ <GeometryDrawing.Pen>
56
+ <Pen Brush="#FFFFD250" Thickness="20" />
57
+ </GeometryDrawing.Pen>
58
+ </GeometryDrawing>
59
+ </DrawingGroup>
60
+ </DrawingImage.Drawing>
61
+ </DrawingImage>
62
+
63
+ <Image x:Key="invert" Source="{StaticResource original}">
64
+ <Image.Effect>
65
+ <local:InvertEffect />
66
+ </Image.Effect>
67
+ </Image>
68
+ </Window.Resources>
69
+
70
+ <UniformGrid
71
+ HorizontalAlignment="Left"
72
+ VerticalAlignment="Top"
73
+ Rows="1">
74
+ <UniformGrid.Resources>
75
+ <Style TargetType="Rectangle">
76
+ <Setter Property="Width" Value="300" />
77
+ <Setter Property="Height" Value="277" />
78
+ <Setter Property="Fill" Value="Blue" />
79
+ </Style>
80
+ </UniformGrid.Resources>
81
+
82
+ <GroupBox Header="original">
83
+ <Image Source="{StaticResource original}" />
84
+ </GroupBox>
85
+
86
+ <GroupBox Header="original - OpacityMask">
87
+ <Viewbox>
88
+ <Rectangle>
89
+ <Rectangle.OpacityMask>
90
+ <ImageBrush ImageSource="{StaticResource original}" />
91
+ </Rectangle.OpacityMask>
92
+ </Rectangle>
93
+ </Viewbox>
94
+ </GroupBox>
95
+
96
+ <GroupBox Header="fake">
97
+ <Viewbox>
98
+ <Grid>
99
+ <Rectangle />
100
+ <Rectangle Fill="White">
101
+ <Rectangle.OpacityMask>
102
+ <ImageBrush ImageSource="{StaticResource original}" />
103
+ </Rectangle.OpacityMask>
104
+ </Rectangle>
105
+ </Grid>
106
+ </Viewbox>
107
+ </GroupBox>
108
+
109
+ <GroupBox Header="invert - OpacityMask">
110
+ <Viewbox>
111
+ <Rectangle>
112
+ <Rectangle.OpacityMask>
113
+ <VisualBrush Visual="{StaticResource invert}" />
114
+ </Rectangle.OpacityMask>
115
+ </Rectangle>
116
+ </Viewbox>
117
+ </GroupBox>
118
+ </UniformGrid>
119
+ </Window>
120
+ ```
121
+
122
+ ```cs
123
+ using System;
124
+ using System.IO;
125
+ using System.Windows;
126
+ using System.Windows.Media;
127
+ using System.Windows.Media.Effects;
128
+
129
+ namespace Questions374313
130
+ {
131
+ public partial class MainWindow : Window
132
+ {
133
+ public MainWindow() => InitializeComponent();
134
+ }
135
+
136
+
137
+ // [wpf - How to Invert Color of XAML PNG Images using C#? - Stack Overflow](https://stackoverflow.com/questions/45093399/how-to-invert-color-of-xaml-png-images-using-c)
138
+ internal class InvertEffect : ShaderEffect
139
+ {
140
+ public Brush Input { get => (Brush)GetValue(InputProperty); set => SetValue(InputProperty, value); }
141
+ public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(InvertEffect), 0);
142
+
143
+ private const string _kshaderAsBase64 = @"AAP///7/HwBDVEFCHAAAAE8AAAAAA///AQAAABwAAAAAAQAASAAAADAAAAADAAAAAQACADgAAAAAAAAAaW5wdXQAq6sEAAwAAQABAAEAAAAAAAAAcHNfM18wAE1pY3Jvc29mdCAoUikgSExTTCBTaGFkZXIgQ29tcGlsZXIgMTAuMQCrUQAABQAAD6AAAIA/AAAAAAAAAAAAAAAAHwAAAgUAAIAAAAOQHwAAAgAAAJAACA+gQgAAAwAAD4AAAOSQAAjkoAIAAAMAAAiAAAD/gQAAAKAFAAADAAgHgAAA/4AAAOSAAQAAAgAICIAAAP+A//8AAA==";
144
+
145
+ private static readonly PixelShader _shader;
146
+
147
+ static InvertEffect()
148
+ {
149
+ _shader = new PixelShader();
150
+ _shader.SetStreamSource(new MemoryStream(Convert.FromBase64String(_kshaderAsBase64)));
151
+ }
152
+
153
+ public InvertEffect()
154
+ {
155
+ PixelShader = _shader;
156
+ UpdateShaderValue(InputProperty);
157
+ }
158
+ }
159
+ }
160
+ ```
161
+
162
+ ```HLSL
163
+ sampler2D input : register(s0);
164
+
165
+ float4 main(float2 uv : TEXCOORD) : COLOR
166
+ {
167
+ float4 color = tex2D(input, uv);
168
+ float alpha = 1 - color.a;
169
+
170
+ // color = 1 - color;
171
+ color.a = alpha;
172
+ color.rgb *= alpha;
173
+
174
+ return color;
175
+ }
176
+ ```
177
+ ぶっちゃけシェーダーなら何でもありみたいなものでしょうから、こんな回りくどいことをしなくてももっと直接適用できるでしょうが^^;
178
+ ![アプリ画像](d90addbe0f36f8336603b85e99a4bbe4.png)
179
+
180
+ ---
181
+
182
+ 途中まで調べていたので回答しましたが、「特に意味はない」と言われてしまうと萎えますね^^;
183
183
  仮絵(`DrawingImage`)を作るのが、一番時間がかかりましたw