Почему при копировании изображения через указатель (BitmapData) картинка отображается не корректно?

Рейтинг: 0Ответов: 0Опубликовано: 20.08.2023

введите сюда описание изображения

Это изображение того, что получается. Код скопирован из документации Microsoft, кроме добавления PictureBox. Так что в нём ошибки маловероятны. Я так думаю что моя проблема связана с преобразованием цветов. Но в чём конкретно понять не получается.

public void DrawTextures()
        {
            // Create a new bitmap.
            Bitmap bmp = new("D:\\Photo");

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                bmp.PixelFormat);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

            // Set every third value to 255. A 24bpp bitmap will look red.  

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.
            bmp.UnlockBits(bmpData);

            PictureBox pictureBox = new()
            {
                BorderStyle = BorderStyle.FixedSingle,
                BackgroundImageLayout = ImageLayout.Stretch,
                Image = bmp,
                Width = 2048,
                Height = 2048
            };
            mainFlowPanel.Controls.Add(pictureBox);
        }

Ответы

Ответов пока нет.