■ WriteableBitmap 클래스에서 비트맵 픽셀을 설정하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="600" Height="450" Title="WriteableBitmap 클래스 : 비트맵 픽셀 설정하기" Loaded="Window_Loaded"> <Grid Name="mainGrid" > </Grid> </Window> |
▶ MainWindow.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 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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { const int BITMAP_WIDTH = 240; const int BITMAP_HEIGHT = 240; WriteableBitmap writeableBitmap = new WriteableBitmap ( BITMAP_WIDTH, BITMAP_HEIGHT, 96, 96, PixelFormats.Bgra32, null ); byte[, ,] pixelArray = new byte[BITMAP_HEIGHT, BITMAP_WIDTH, 4]; for(int y = 0; y < BITMAP_HEIGHT; y++) { for(int x = 0; x < BITMAP_WIDTH; x++) { for(int i = 0; i < 3; i++) { pixelArray[y, x, i] = 0; } pixelArray[y, x, 3] = 255; } } // 파란색 for(int y = 0; y < 80; y++) { for(int x = 0; x <= y; x++) { pixelArray[y, x, 0] = 255; } } // 녹색 for(int y = 80; y < 160; y++) { for(int x = 0; x < 80; x++) { pixelArray[y, x, 1] = 255; } } // 빨간색 for(int y = 160; y < 240; y++) { for(int x = 0; x < 80; x++) { pixelArray[y, x, 2] = 255; } } byte[] byteArray = new byte[BITMAP_HEIGHT * BITMAP_WIDTH * 4]; int index = 0; for(int row = 0; row < BITMAP_HEIGHT; row++) { for(int col = 0; col < BITMAP_WIDTH; col++) { for(int i = 0; i < 4; i++) { byteArray[index++]= pixelArray[row, col, i]; } } } Int32Rect rectangle = new Int32Rect(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT); int stride = 4 * BITMAP_WIDTH; writeableBitmap.WritePixels(rectangle, byteArray, stride, 0); Image image = new Image(); image.Stretch = Stretch.None; image.Margin = new Thickness(0); this.mainGrid.Children.Add(image); image.Source = writeableBitmap; } #endregion } } |