回答編集履歴

1

見直しキャンペーン中

2023/07/29 12:42

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,365 +1,183 @@
1
1
  普通に考えるとこの辺を使って、アルファを反転させた画像を作るんでしょうね。
2
-
3
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)
4
-
5
-
6
3
 
7
4
  [WPFにおける画像のピクセル値への高速アクセス - Qiita](https://qiita.com/MaruiMisato/items/b350979f35bc0e9e0f51)
8
5
 
9
-
10
-
11
6
  わたしはこういった(めんどくさそうな)ことは嫌いなので、調べている途中で見たこれでやってみました。
12
-
13
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)
14
8
 
15
-
16
-
17
9
  注)私はシェーダーについて何ひとつわかっていません。
18
-
19
- ```xaml
10
+ ```xml
20
-
21
11
  <Window
22
-
23
12
  x:Class="Questions374313.MainWindow"
24
-
25
13
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
-
27
14
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
-
29
15
  xmlns:local="clr-namespace:Questions374313"
30
-
31
16
  Width="800"
32
-
33
17
  Height="450"
34
-
35
18
  Background="MistyRose">
36
-
37
19
  <Window.Resources>
38
20
 
39
-
40
-
41
21
  <!--<BitmapImage x:Key="original" UriSource="orange.png" />-->
42
-
43
22
  <!-- オレンジ画像を使う場合↑を生かして、↓をコメント化 -->
44
-
45
23
  <DrawingImage x:Key="original">
46
-
47
24
  <DrawingImage.Drawing>
48
-
49
25
  <DrawingGroup>
50
-
51
26
  <GeometryDrawing Brush="Transparent">
52
-
53
27
  <GeometryDrawing.Geometry>
54
-
55
28
  <RectangleGeometry Rect="0,0,300,277" />
56
-
57
29
  </GeometryDrawing.Geometry>
58
-
59
30
  </GeometryDrawing>
60
-
61
31
  <GeometryDrawing>
62
-
63
32
  <GeometryDrawing.Geometry>
64
-
65
33
  <GeometryGroup>
66
-
67
34
  <EllipseGeometry
68
-
69
35
  Center="150,138.5"
70
-
71
36
  RadiusX="140"
72
-
73
37
  RadiusY="100">
74
-
75
38
  <EllipseGeometry.Transform>
76
-
77
39
  <TransformGroup>
78
-
79
40
  <ScaleTransform CenterX="150" CenterY="138.5" />
80
-
81
41
  <SkewTransform CenterX="150" CenterY="138.5" />
82
-
83
42
  <RotateTransform Angle="140" CenterX="150" CenterY="138.5" />
84
-
85
43
  <TranslateTransform />
86
-
87
44
  </TransformGroup>
88
-
89
45
  </EllipseGeometry.Transform>
90
-
91
46
  </EllipseGeometry>
92
-
93
47
  </GeometryGroup>
94
-
95
48
  </GeometryDrawing.Geometry>
96
-
97
49
  <GeometryDrawing.Brush>
98
-
99
50
  <LinearGradientBrush>
100
-
101
51
  <GradientStop Offset="0.0" Color="#FFDD6200" />
102
-
103
52
  <GradientStop Offset="1.0" Color="#0000" />
104
-
105
53
  </LinearGradientBrush>
106
-
107
54
  </GeometryDrawing.Brush>
108
-
109
55
  <GeometryDrawing.Pen>
110
-
111
56
  <Pen Brush="#FFFFD250" Thickness="20" />
112
-
113
57
  </GeometryDrawing.Pen>
114
-
115
58
  </GeometryDrawing>
116
-
117
59
  </DrawingGroup>
118
-
119
60
  </DrawingImage.Drawing>
120
-
121
61
  </DrawingImage>
122
62
 
123
-
124
-
125
63
  <Image x:Key="invert" Source="{StaticResource original}">
126
-
127
64
  <Image.Effect>
128
-
129
65
  <local:InvertEffect />
130
-
131
66
  </Image.Effect>
132
-
133
67
  </Image>
134
-
135
68
  </Window.Resources>
136
69
 
137
-
138
-
139
70
  <UniformGrid
140
-
141
71
  HorizontalAlignment="Left"
142
-
143
72
  VerticalAlignment="Top"
144
-
145
73
  Rows="1">
146
-
147
74
  <UniformGrid.Resources>
148
-
149
75
  <Style TargetType="Rectangle">
150
-
151
76
  <Setter Property="Width" Value="300" />
152
-
153
77
  <Setter Property="Height" Value="277" />
154
-
155
78
  <Setter Property="Fill" Value="Blue" />
156
-
157
79
  </Style>
158
-
159
80
  </UniformGrid.Resources>
160
81
 
161
-
162
-
163
82
  <GroupBox Header="original">
164
-
165
83
  <Image Source="{StaticResource original}" />
166
-
167
84
  </GroupBox>
168
85
 
169
-
170
-
171
86
  <GroupBox Header="original - OpacityMask">
172
-
173
87
  <Viewbox>
174
-
175
88
  <Rectangle>
176
-
177
89
  <Rectangle.OpacityMask>
178
-
179
90
  <ImageBrush ImageSource="{StaticResource original}" />
180
-
181
91
  </Rectangle.OpacityMask>
182
-
183
92
  </Rectangle>
184
-
185
93
  </Viewbox>
186
-
187
94
  </GroupBox>
188
95
 
189
-
190
-
191
96
  <GroupBox Header="fake">
192
-
193
97
  <Viewbox>
194
-
195
98
  <Grid>
196
-
197
99
  <Rectangle />
198
-
199
100
  <Rectangle Fill="White">
200
-
201
101
  <Rectangle.OpacityMask>
202
-
203
102
  <ImageBrush ImageSource="{StaticResource original}" />
204
-
205
103
  </Rectangle.OpacityMask>
206
-
207
104
  </Rectangle>
208
-
209
105
  </Grid>
210
-
211
106
  </Viewbox>
212
-
213
107
  </GroupBox>
214
108
 
215
-
216
-
217
109
  <GroupBox Header="invert - OpacityMask">
218
-
219
110
  <Viewbox>
220
-
221
111
  <Rectangle>
222
-
223
112
  <Rectangle.OpacityMask>
224
-
225
113
  <VisualBrush Visual="{StaticResource invert}" />
226
-
227
114
  </Rectangle.OpacityMask>
228
-
229
115
  </Rectangle>
230
-
231
116
  </Viewbox>
232
-
233
117
  </GroupBox>
234
-
235
118
  </UniformGrid>
236
-
237
119
  </Window>
238
-
239
120
  ```
240
121
 
241
-
242
-
243
- ```C#
122
+ ```cs
244
-
245
123
  using System;
246
-
247
124
  using System.IO;
248
-
249
125
  using System.Windows;
250
-
251
126
  using System.Windows.Media;
252
-
253
127
  using System.Windows.Media.Effects;
254
128
 
255
-
256
-
257
129
  namespace Questions374313
258
-
259
130
  {
260
-
261
131
  public partial class MainWindow : Window
262
-
263
132
  {
264
-
265
133
  public MainWindow() => InitializeComponent();
266
-
267
134
  }
268
135
 
269
136
 
270
-
271
-
272
-
273
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)
274
-
275
138
  internal class InvertEffect : ShaderEffect
276
-
277
139
  {
278
-
279
140
  public Brush Input { get => (Brush)GetValue(InputProperty); set => SetValue(InputProperty, value); }
280
-
281
141
  public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(InvertEffect), 0);
282
-
283
-
284
142
 
285
143
  private const string _kshaderAsBase64 = @"AAP///7/HwBDVEFCHAAAAE8AAAAAA///AQAAABwAAAAAAQAASAAAADAAAAADAAAAAQACADgAAAAAAAAAaW5wdXQAq6sEAAwAAQABAAEAAAAAAAAAcHNfM18wAE1pY3Jvc29mdCAoUikgSExTTCBTaGFkZXIgQ29tcGlsZXIgMTAuMQCrUQAABQAAD6AAAIA/AAAAAAAAAAAAAAAAHwAAAgUAAIAAAAOQHwAAAgAAAJAACA+gQgAAAwAAD4AAAOSQAAjkoAIAAAMAAAiAAAD/gQAAAKAFAAADAAgHgAAA/4AAAOSAAQAAAgAICIAAAP+A//8AAA==";
286
144
 
287
-
288
-
289
145
  private static readonly PixelShader _shader;
290
146
 
291
-
292
-
293
147
  static InvertEffect()
294
-
295
148
  {
296
-
297
149
  _shader = new PixelShader();
298
-
299
150
  _shader.SetStreamSource(new MemoryStream(Convert.FromBase64String(_kshaderAsBase64)));
300
-
301
151
  }
302
152
 
303
-
304
-
305
153
  public InvertEffect()
306
-
307
154
  {
308
-
309
155
  PixelShader = _shader;
310
-
311
156
  UpdateShaderValue(InputProperty);
312
-
313
157
  }
314
-
315
158
  }
316
-
317
159
  }
318
-
319
160
  ```
320
161
 
321
-
322
-
323
162
  ```HLSL
324
-
325
163
  sampler2D input : register(s0);
326
164
 
327
-
328
-
329
165
  float4 main(float2 uv : TEXCOORD) : COLOR
330
-
331
166
  {
332
-
333
167
  float4 color = tex2D(input, uv);
334
-
335
168
  float alpha = 1 - color.a;
336
169
 
337
-
338
-
339
170
  // color = 1 - color;
340
-
341
171
  color.a = alpha;
342
-
343
172
  color.rgb *= alpha;
344
173
 
345
-
346
-
347
174
  return color;
348
-
349
175
  }
350
-
351
176
  ```
352
-
353
177
  ぶっちゃけシェーダーなら何でもありみたいなものでしょうから、こんな回りくどいことをしなくてももっと直接適用できるでしょうが^^;
354
-
355
178
  ![アプリ画像](d90addbe0f36f8336603b85e99a4bbe4.png)
356
-
357
-
358
179
 
359
180
  ---
360
181
 
361
-
362
-
363
182
  途中まで調べていたので回答しましたが、「特に意味はない」と言われてしまうと萎えますね^^;
364
-
365
183
  仮絵(`DrawingImage`)を作るのが、一番時間がかかりましたw