Example #1
0
        private static Texture2D CreateTexture2DFromBitmap(Bitmap bitmap, TextureFormat format, bool generateMipmaps, TextureTarget textureTarget)
        {
            using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                                BitmapAlgorithms.SizeOfPixelsInBytes(bitmap)))
            {
                pixelBuffer.CopyFromBitmap(bitmap);

                Texture2DDescription description = new Texture2DDescription(bitmap.Width, bitmap.Height, format, generateMipmaps);
                Texture2D            texture     = new Texture2DGL3x(description, textureTarget);
                texture.CopyFromBuffer(pixelBuffer,
                                       TextureUtility.ImagingPixelFormatToImageFormat(bitmap.PixelFormat),
                                       TextureUtility.ImagingPixelFormatToDatatype(bitmap.PixelFormat));

                return(texture);
            }
        }
        public void CopyFromBitmap(Bitmap bitmap)
        {
            Bitmap lockedBitmap;

            if (BitmapAlgorithms.RowOrder(bitmap) == ImageRowOrder.TopToBottom)
            {
                //
                // OpenGL wants rows bottom to top.
                //
                Bitmap flippedBitmap = (Bitmap)bitmap.Clone();
                flippedBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                lockedBitmap = flippedBitmap;
            }
            else
            {
                lockedBitmap = bitmap;
            }

            BitmapData lockedPixels = lockedBitmap.LockBits(new Rectangle(
                                                                0, 0, lockedBitmap.Width, lockedBitmap.Height),
                                                            ImageLockMode.ReadOnly, lockedBitmap.PixelFormat);

            int sizeInBytes = lockedPixels.Stride * lockedPixels.Height;

            if (sizeInBytes > _sizeInBytes)
            {
                throw new ArgumentException(
                          "bitmap's size in bytes must be less than or equal to SizeInBytes.",
                          "bitmap");
            }

            Bind();
            GL.BufferSubData(_type,
                             new IntPtr(),
                             new IntPtr(sizeInBytes),
                             lockedPixels.Scan0);

            lockedBitmap.UnlockBits(lockedPixels);
        }
        public void WritePixelBufferBitmap()
        {
            using (Bitmap bitmap = new Bitmap(1, 1))
                using (GraphicsWindow window = Device.CreateWindow(1, 1))
                    using (WritePixelBuffer pixelBuffer = Device.CreateWritePixelBuffer(PixelBufferHint.Stream,
                                                                                        BitmapAlgorithms.SizeOfPixelsInBytes(bitmap)))
                    {
                        Color color = Color.FromArgb(0, 1, 2, 3);
                        bitmap.SetPixel(0, 0, color);

                        pixelBuffer.CopyFromBitmap(bitmap);

                        //
                        // Verify read back - comes back BGRA
                        //
                        BlittableBGRA[] readBackPixel = pixelBuffer.CopyToSystemMemory <BlittableBGRA>();
                        Assert.AreEqual(color.R, readBackPixel[0].R);
                        Assert.AreEqual(color.G, readBackPixel[0].G);
                        Assert.AreEqual(color.B, readBackPixel[0].B);
                        Assert.AreEqual(color.A, readBackPixel[0].A);

                        //
                        // Verify read back into bitmap
                        //
                        using (Bitmap readBackBitmap = pixelBuffer.CopyToBitmap(1, 1, PixelFormat.Format32bppArgb))
                        {
                            Assert.AreEqual(color, readBackBitmap.GetPixel(0, 0));
                        }
                    }
        }