첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ WriteableBitmap 클래스 : 비트맵 픽셀 설정하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<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

 

 

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

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요