Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new DirectXTexture class.
        /// </summary>
        /// <param name="bmp">The Bitmap.</param>
        internal DirectXTexture(System.Drawing.Bitmap bmp)
        {
            RawBitmap = (System.Drawing.Bitmap)bmp.Clone();
            _width    = bmp.Width;
            _height   = bmp.Height;
            var sourceArea       = new Rectangle(0, 0, bmp.Width, bmp.Height);
            var bitmapProperties = new BitmapProperties
            {
                PixelFormat   = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied),
                HorizontalDpi = 96,
                VerticalDpi   = 96
            };
            var size = new Size(bmp.Width, bmp.Height);

            int stride = bmp.Width * sizeof(int);

            using (var tempStream = new DataStream(bmp.Height * stride, true, true))
            {
                BitmapData bitmapData = bmp.LockBits(sourceArea, ImageLockMode.ReadOnly,
                                                     System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                for (int y = 0; y < bmp.Height; y++)
                {
                    int offset = bitmapData.Stride * y;
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        byte b    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        byte g    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        byte r    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        byte a    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        int  rgba = r | (g << 8) | (b << 16) | (a << 24);
                        tempStream.Write(rgba);
                    }
                }
                bmp.UnlockBits(bitmapData);
                tempStream.Position = 0;
                _bmp = new Bitmap(DirectXHelper.RenderTarget, size, tempStream, stride, bitmapProperties);
            }
        }
Exemplo n.º 2
0
        public Bitmap GetBitmap(string image)
        {
            Bitmap bitmap;
            bitmaps.TryGetValue(image, out bitmap);
            if (null == bitmap)
            {
                using (System.Drawing.Bitmap source = new System.Drawing.Bitmap(Path.Combine("Assets", image)))
                {
                    System.Drawing.Imaging.BitmapData sourceData = source.LockBits(
                        new Rectangle(0, 0, source.Width, source.Height),
                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb
                    );

                    using (DataStream dataStream = new DataStream(sourceData.Scan0, sourceData.Stride * sourceData.Height, true, false))
                    {
                        bitmap = new Bitmap(renderTarget, new Size(source.Width, source.Height), dataStream, sourceData.Stride, bitmapProperties);
                        bitmaps[image] = bitmap;
                    }

                    source.UnlockBits(sourceData);
                }
            }
            return bitmap;
        }