# xaml 

    <Grid Background="Black">
        <Image x:Name="DrawCanvas" Width="100" Height="100"/>
    </Grid>

 

# 비트맵 변경

        public void init(int w, int h)
        {
            DrawCanvas.Width = w;
            DrawCanvas.Height = h;
        }


        public void DrawColorCanvas2(int x, int y, int color)
        {
            int bitmapWidth = (int)DrawCanvas.Width;
            int bitmapHeight = (int)DrawCanvas.Height;

            if (_writeableBitmap == null)
            {
                _writeableBitmap = new WriteableBitmap(bitmapWidth, bitmapHeight, 96, 96, PixelFormats.Bgra32, null);

                int length = _writeableBitmap.BackBufferStride * bitmapHeight;
                byte[] buffer = new byte[length];

                _writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)_writeableBitmap.Width, (int)_writeableBitmap.Height), buffer, _writeableBitmap.BackBufferStride, 0);
                DrawCanvas.Source = _writeableBitmap;
            }

            try
            {
                // Reserve the back buffer for updates.
                _writeableBitmap.Lock();

                // Compute the pixel's color.
                int color_data = 0;
                color_data |= color << 0;   // B
                color_data |= color << 8;   // G
                color_data |= color << 16;  // R
                color_data |= 0xff << 24;   // A 

                unsafe
                {
                    int length = _writeableBitmap.BackBufferStride * bitmapHeight;

                    //for (int y = 0; y < bitmapHeight; y++)
                    {
                        //for (int x = 0; x < bitmapWidth; x++)
                        {
                            int i = (y * _writeableBitmap.BackBufferStride) + (x * 4);

                            IntPtr pBackBuffer = _writeableBitmap.BackBuffer;

                            // Find the address of the pixel to draw.
                            pBackBuffer += y * _writeableBitmap.BackBufferStride;
                            pBackBuffer += x * 4;

                            // Assign the color data to the pixel.
                            *((int*)pBackBuffer) = color_data;
                        }
                    }
                }

                // Specify the area of the bitmap that changed.
                _writeableBitmap.AddDirtyRect(new Int32Rect(x, y, 1, 1));
            }
            finally
            {
                // Release the back buffer and make it available for display.
                _writeableBitmap.Unlock();
            }
        }

 

# 테스트

        public void Test()
        {
            int bitmapWidth = (int)DrawCanvas.Width;
            int bitmapHeight = (int)DrawCanvas.Height;

            for (int y = 0; y < bitmapHeight; y++)
            {
                for (int x = 0; x < bitmapWidth; x++)
                {
                    DrawColorCanvas2(x, y, 0, false);
                }
            }

            // 화면 업데이트
            System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(
                            System.Windows.Threading.DispatcherPriority.Background
                           , new System.Threading.ThreadStart(delegate { }));
        }

 

'Programming > WPF' 카테고리의 다른 글

[무료] Serial Port List 프로그램 (windows)  (0) 2023.10.26

+ Recent posts