■ XamlCompositionBrushBase 클래스를 사용해 커스텀 혼합 브러시를 만드는 방법을 보여준다.
▶ CustomCompositionBrush.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
using Microsoft.Graphics.Canvas; using Microsoft.Graphics.Canvas.Effects; using System; using Windows.UI.Composition; using Windows.UI.Xaml; using Windows.UI.Xaml.Media; using Windows.Storage.Streams; namespace TestProject { /// <summary> /// 커스텀 혼합 브러시 /// </summary> public class CustomCompositionBrush : XamlCompositionBrushBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 불선명도 양 속성 - BlurAmountProperty /// <summary> /// 불선명도 양 속성 /// </summary> public static readonly DependencyProperty BlurAmountProperty = DependencyProperty.Register ( "BlurAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 0.0, BlurAmountPropertyChangedCallback ) ); #endregion #region 대비 양 속성 - ContrastAmountProperty /// <summary> /// 대비 양 속성 /// </summary> public static readonly DependencyProperty ContrastAmountProperty = DependencyProperty.Register ( "ContrastAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 0.0, new PropertyChangedCallback(ContrastAmountPropertyChangedCallback) ) ); #endregion #region 채도 양 속성 - SaturationAmountProperty /// <summary> /// 채도 양 속성 /// </summary> public static readonly DependencyProperty SaturationAmountProperty = DependencyProperty.Register ( "SaturationAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 1.0, new PropertyChangedCallback(SaturationAmountPropertyChangedCallback) ) ); #endregion #region 노출 양 속성 - ExposureAmountProperty /// <summary> /// 노출 양 속성 /// </summary> public static readonly DependencyProperty ExposureAmountProperty = DependencyProperty.Register ( "ExposureAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 0.0, new PropertyChangedCallback(ExposureAmountPropertyChangedCallback) ) ); #endregion #region 색조 양 속성 - TintAmountProperty /// <summary> /// 색조 양 속성 /// </summary> public static readonly DependencyProperty TintAmountProperty = DependencyProperty.Register ( "TintAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 0.0, new PropertyChangedCallback(TintAmountPropertyChangedCallback) ) ); #endregion #region 온도 양 속성 - TemperatureAmountProperty /// <summary> /// 온도 양 속성 /// </summary> public static readonly DependencyProperty TemperatureAmountProperty = DependencyProperty.Register ( "TemperatureAmount", typeof(double), typeof(CustomCompositionBrush), new PropertyMetadata ( 0.0, new PropertyChangedCallback(TemperatureAmountPropertyChangedCallback) ) ); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이미지 로딩 여부 /// </summary> private bool isImageLoading = false; /// <summary> /// 로드된 이미지 표면 /// </summary> private LoadedImageSurface loadedImageSurface; /// <summary> /// 혼합 효과 브러시 /// </summary> private CompositionEffectBrush compositionEffectBrush; /// <summary> /// 대비 효과 /// </summary> private ContrastEffect contrastEffect; /// <summary> /// 노출 효과 /// </summary> private ExposureEffect exposureEffect; /// <summary> /// 온도/색조 효과 /// </summary> private TemperatureAndTintEffect temperatureAndTintEffect; /// <summary> /// 가우스안 불선명 효과 /// </summary> private GaussianBlurEffect gaussianBlurEffect; /// <summary> /// 채도 효과 /// </summary> private SaturationEffect saturationEffect; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이미지 - Image /// <summary> /// 이미지 /// </summary> public ICanvasImage Image { get { this.contrastEffect.Contrast = (float)ContrastAmount; this.exposureEffect.Exposure = (float)ExposureAmount; this.temperatureAndTintEffect.Tint = (float)TintAmount; this.temperatureAndTintEffect.Temperature = (float)TemperatureAmount; this.saturationEffect.Saturation = (float)SaturationAmount; this.gaussianBlurEffect.BlurAmount = (float)BlurAmount; return this.gaussianBlurEffect; } } #endregion #region 불선명도 양 - BlurAmount /// <summary> /// 불선명도 양 /// </summary> public double BlurAmount { get { return (double)GetValue(BlurAmountProperty); } set { SetValue(BlurAmountProperty, value); } } #endregion #region 대비 양 - ContrastAmount /// <summary> /// 대비 양 /// </summary> public double ContrastAmount { get { return (double)GetValue(ContrastAmountProperty); } set { SetValue(ContrastAmountProperty, value); } } #endregion #region 채도 양 - SaturationAmount /// <summary> /// 채도 양 /// </summary> public double SaturationAmount { get { return (double)GetValue(SaturationAmountProperty); } set { SetValue(SaturationAmountProperty, value); } } #endregion #region 노출 양 - ExposureAmount /// <summary> /// 노출 양 /// </summary> public double ExposureAmount { get { return (double)GetValue(ExposureAmountProperty); } set { SetValue(ExposureAmountProperty, value); } } #endregion #region 색조 양 - TintAmount /// <summary> /// 색조 양 /// </summary> public double TintAmount { get { return (double)GetValue(TintAmountProperty); } set { SetValue(TintAmountProperty, value); } } #endregion #region 온도 양 - TemperatureAmount /// <summary> /// 온도 양 /// </summary> public double TemperatureAmount { get { return (double)GetValue(TemperatureAmountProperty); } set { SetValue(TemperatureAmountProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CustomCompositionBrush() /// <summary> /// 생성자 /// </summary> public CustomCompositionBrush() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 불선명도 양 속성 변경시 콜백 처리하기 - BlurAmountPropertyChangedCallback(d, e) /// <summary> /// 불선명도 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void BlurAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("Blur.BlurAmount", (float)(double)e.NewValue); } #endregion #region 대비 양 속성 변경시 콜백 처리하기 - ContrastAmountPropertyChangedCallback(d, e) /// <summary> /// 대비 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void ContrastAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("ContrastEffect.Contrast", (float)(double)e.NewValue); } #endregion #region 채도 양 속성 변경시 콜백 처리하기 - SaturationAmountPropertyChangedCallback(d, e) /// <summary> /// 채도 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void SaturationAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("SaturationEffect.Saturation", (float)(double)e.NewValue); } #endregion #region 노출 양 속성 변경시 콜백 처리하기 - ExposureAmountPropertyChangedCallback(d, e) /// <summary> /// 노출 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void ExposureAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("ExposureEffect.Exposure", (float)(double)e.NewValue); } #endregion #region 색조 양 속성 변경시 콜백 처리하기 - TintAmountPropertyChangedCallback(d, e) /// <summary> /// 색조 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void TintAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("TemperatureAndTintEffect.Tint", (float)(double)e.NewValue); } #endregion #region 온도 양 속성 변경시 콜백 처리하기 - TemperatureAmountPropertyChangedCallback(d, e) /// <summary> /// 온도 양 속성 변경시 콜백 처리하기 /// </summary> /// <param name="d">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void TemperatureAmountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomCompositionBrush brush = d as CustomCompositionBrush; brush.CompositionBrush?.Properties.InsertScalar("TemperatureAndTintEffect.Temperature", (float)(double)e.NewValue); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 소스 설정하기 - SetSource(source) /// <summary> /// 소스 설정하기 /// </summary> /// <param name="source">소스</param> public void SetSource(ICanvasImage source) { this.saturationEffect.Source = source; } #endregion #region 이미지 로드하기 - LoadImage(filePath) /// <summary> /// 이미지 로드하기 /// </summary> /// <param name="filePath">파일 경로</param> public void LoadImage(string filePath) { Compositor compositor = Window.Current.Compositor; this.loadedImageSurface = LoadedImageSurface.StartLoadFromUri(new Uri(filePath)); this.loadedImageSurface.LoadCompleted += loadedImageSurface_LoadedCompleted; } #endregion #region 이미지 로드하기 - LoadImage(stream) /// <summary> /// 이미지 로드하기 /// </summary> /// <param name="stream">스트림</param> public void LoadImage(IRandomAccessStream stream) { if(stream != null && this.isImageLoading == false) { Compositor compositor = Window.Current.Compositor; this.isImageLoading = true; this.loadedImageSurface = LoadedImageSurface.StartLoadFromStream(stream); this.loadedImageSurface.LoadCompleted += loadedImageSurface_LoadedCompleted; } } #endregion //////////////////////////////////////////////////////////////////////////////// Protected #region 연결 취소시 처리하기 - OnDisconnected() /// <summary> /// 연결 취소시 처리하기 /// </summary> protected override void OnDisconnected() { if(this.loadedImageSurface != null) { this.loadedImageSurface.Dispose(); this.loadedImageSurface = null; } if(CompositionBrush != null) { CompositionBrush.Dispose(); CompositionBrush = null; } } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 로드된 이미지 표면 로드 완료시 처리하기 - loadedImageSurface_LoadedCompleted(sender, e) /// <summary> /// 로드된 이미지 표면 로드 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void loadedImageSurface_LoadedCompleted(LoadedImageSurface sender, LoadedImageSourceLoadCompletedEventArgs e) { this.isImageLoading = false; if(e.Status == LoadedImageSourceLoadStatus.Success) { Compositor compositor = Window.Current.Compositor; CompositionSurfaceBrush brush = compositor.CreateSurfaceBrush(this.loadedImageSurface); brush.Stretch = CompositionStretch.UniformToFill; this.saturationEffect = new SaturationEffect() { Name = "SaturationEffect", Saturation = (float)SaturationAmount, Source = new CompositionEffectSourceParameter("image") }; this.contrastEffect = new ContrastEffect() { Name = "ContrastEffect", Contrast = (float)ContrastAmount, Source = this.saturationEffect }; this.exposureEffect = new ExposureEffect() { Name = "ExposureEffect", Source = this.contrastEffect, Exposure = (float)ExposureAmount, }; this.temperatureAndTintEffect = new TemperatureAndTintEffect() { Name = "TemperatureAndTintEffect", Source = this.exposureEffect, Temperature = (float)TemperatureAmount, Tint = (float)TintAmount }; this.gaussianBlurEffect = new GaussianBlurEffect() { Name = "Blur", Source = this.temperatureAndTintEffect, BlurAmount = (float)BlurAmount, BorderMode = EffectBorderMode.Hard, }; CompositionEffectFactory factory = compositor.CreateEffectFactory ( this.gaussianBlurEffect, new[] { "SaturationEffect.Saturation", "ExposureEffect.Exposure", "Blur.BlurAmount", "TemperatureAndTintEffect.Temperature", "TemperatureAndTintEffect.Tint", "ContrastEffect.Contrast" } ); this.compositionEffectBrush = factory.CreateBrush(); this.compositionEffectBrush.SetSourceParameter("image", brush); CompositionBrush = this.compositionEffectBrush; } else { LoadImage("ms-appx:///Assets/StoreLogo.png"); } } #endregion } } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestProject" RequestedTheme="Dark" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Rectangle Width="200" Height="200"> <Rectangle.Fill> <local:CustomCompositionBrush x:Name="customCompositionBrush" BlurAmount="0" ContrastAmount="0" SaturationAmount="0" ExposureAmount="0" TintAmount="0" TemperatureAmount="0" /> </Rectangle.Fill> </Rectangle> <Slider Header="불선명도" Width="500" Minimum="0" Maximum="5" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.BlurAmount, Mode=TwoWay}" /> <Slider Header="대비" Margin="0 10 0 0" Width="500" Minimum="-1" Maximum="1" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.ContrastAmount, Mode=TwoWay}" /> <Slider Header="채도" Margin="0 10 0 0" Width="500" Minimum="-2" Maximum="2" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.SaturationAmount, Mode=TwoWay}" /> <Slider Header="노출" Margin="0 10 0 0" Width="500" Minimum="-2" Maximum="2" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.ExposureAmount, Mode=TwoWay}" /> <Slider Header="색조" Margin="0 10 0 0" Width="500" Minimum="-1" Maximum="1" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.TintAmount, Mode=TwoWay}" /> <Slider Header="온도" Margin="0 10 0 0" Width="500" Minimum="-1" Maximum="1" StepFrequency="0.1" TickFrequency="0.1" Value="{x:Bind customCompositionBrush.TemperatureAmount, Mode=TwoWay}" /> </StackPanel> </Grid> </Page> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using Windows.ApplicationModel; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 탐색되는 경우 처리하기 - OnNavigatedTo(e) /// <summary> /// 탐색되는 경우 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override async void OnNavigatedTo(NavigationEventArgs e) { StorageFolder packageStorageFolder = Package.Current.InstalledLocation; StorageFolder assetsStorageFolder = await packageStorageFolder.GetFolderAsync("Assets"); StorageFile storageFile = await assetsStorageFolder.GetFileAsync("Square150x150Logo.scale-200.png"); using(IRandomAccessStream stream = await storageFile.OpenReadAsync()) { this.customCompositionBrush.LoadImage(stream); } } #endregion } } |