Exemplo n.º 1
0
        public static Bitmap loadBitmapFromFile(string filename)
        {
            using (var bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(filename))
            {
                var sourceArea = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bp         = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
                var size       = new Size2(bitmap.Width, bitmap.Height);

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

                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < bitmap.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);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;
                    return(new Bitmap(rt, size, tempStream, stride, bp));
                }
            }
        }
Exemplo n.º 2
0
        public Bitmap ConvertBitmap(System.Drawing.Bitmap bmp)
        {
            System.Drawing.Imaging.BitmapData bmpData =
                bmp.LockBits(
                    new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            DataStream       stream   = new DataStream(bmpData.Scan0, bmpData.Stride * bmpData.Height, true, false);
            PixelFormat      pFormat  = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
            BitmapProperties bmpProps = new BitmapProperties(pFormat);

            Bitmap result =
                new Bitmap(
                    renderTarget,
                    new Size2(bmp.Width, bmp.Height),
                    stream,
                    bmpData.Stride,
                    bmpProps);

            bmp.UnlockBits(bmpData);

            stream.Dispose();
            bmp.Dispose();

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Creates a Direct2D bitmap from a GDI bitmap for the specified rendering target
        /// </summary>
        /// <param name="bitmap">Original bitmap</param>
        /// <param name="renderTarget">Destination rendering target</param>
        /// <param name="disposeOriginal">Whether or not to release the resources of the GDI bitmap</param>
        /// <returns>A Direct2D bitmap instance the caller is responsible for disposal</returns>
        public static Bitmap ToDirect2DBitmap(
            this System.Drawing.Bitmap bitmap,
            RenderTarget renderTarget,
            bool disposeOriginal = true)
        {
            // lock bits from the original bitmap so we can read its data
            BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                              ImageLockMode.ReadOnly,
                                              PixelFormat.Format32bppPArgb);

            // copy GDI bitmap data to Direct2D one
            var stream = new DataStream(data.Scan0, data.Stride * data.Height, true, false);
            var format = new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
            var props  = new BitmapProperties(format);

            // create Direct2D bitmap and release resources
            var direct2DBitmap = new Bitmap(renderTarget,
                                            new Size2(bitmap.Width, bitmap.Height),
                                            stream,
                                            data.Stride,
                                            props);

            // release resources
            stream.Dispose();
            bitmap.UnlockBits(data);
            if (disposeOriginal)
            {
                bitmap.Dispose();
            }

            // return new bitmap
            return(direct2DBitmap);
        }
Exemplo n.º 4
0
        } // Resize()

        internal static Bitmap GetDirect2DBitmapWithSharpDX(RenderTarget rt, System.Drawing.Bitmap image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            if (image.PixelFormat != GdiPixelFormat.Format32bppArgb)
            {
                return(null);
            }

            var imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                                           System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);


            var dataStream = new DataStream(imageData.Scan0, imageData.Stride * imageData.Height, true, false);
            var properties = new BitmapProperties
            {
                PixelFormat = new SharpDX.Direct2D1.PixelFormat
                {
                    Format    = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    AlphaMode = AlphaMode.Premultiplied
                }
            };


            //Load the image from the gdi resource
            var bitmapDirect2D = new Bitmap(rt, new Size2(image.Width, image.Height), dataStream, imageData.Stride, properties);

            image.UnlockBits(imageData);

            return(bitmapDirect2D);
        } // GetDirect2DBitmapWithSharpDX()
Exemplo n.º 5
0
        /// <summary>
        /// Convert a <see cref="System.Drawing.Bitmap"/> to a <see cref="SharpDX.Direct2D1.Bitmap"/>.
        /// </summary>
        /// <param name="bitmap">The value to convert.</param>
        /// <param name="renderTarget">The Direct2D1 RenderTarget.</param>
        /// <returns>The converted value.</returns>
        public static SharpDX.Direct2D1.Bitmap ToDxBitmap(this System.Drawing.Bitmap bitmap, RenderTarget renderTarget)
        {
            var sourceArea       = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
            var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
            var size             = new SharpDX.Size2(bitmap.Width, bitmap.Height);

            // Transform pixels from BGRA to RGBA
            int stride = bitmap.Width * sizeof(int);

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

                // Convert all pixels
                for (int y = 0; y < bitmap.Height; y++)
                {
                    int offset = bitmapData.Stride * y;
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        // Not optimized
                        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);
                    }
                }
                bitmap.UnlockBits(bitmapData);
                tempStream.Position = 0;

                return(new SharpDX.Direct2D1.Bitmap(renderTarget, size, tempStream, stride, bitmapProperties));
            }
        }
Exemplo n.º 6
0
        private SharpDX.Direct2D1.Bitmap LoadFromFile(RenderTarget device, System.Drawing.Bitmap res)
        {
            using (var bitmap = new System.Drawing.Bitmap(res))
            {
                var sourceArea       = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bitmapProperties = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
                var size             = new Size2(bitmap.Width, bitmap.Height);

                int stride = bitmap.Width * sizeof(int);
                using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
                {
                    var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < bitmap.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);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;

                    return(new SharpDX.Direct2D1.Bitmap(device, size, tempStream, stride, bitmapProperties));
                }
            }
        }
Exemplo n.º 7
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(
                new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), 96, 96);
            var size = new Size2(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.º 8
0
        private void UpdateMapFallback(string continent)
        {
            var textureData = new uint[Width * Height];

            for (var i = 0; i < 64; ++i)
            {
                for (var j = 0; j < 64; ++j)
                {
                    var exists = FileManager.Instance.Provider.Exists(string.Format(@"World\Maps\{0}\{0}_{1}_{2}.adt", continent, j, i));
                    if (exists)
                    {
                        for (var k = 0; k < 17; ++k)
                        {
                            for (var l = 0; l < 17; ++l)
                            {
                                textureData[(i * 17 + k) * (64 * 17) + j * 17 + l] = 0xFFFFFFFF;
                            }
                        }
                    }
                }
            }

            var bmpProps =
                new BitmapProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                     AlphaMode.Ignore));

            using (var dataStream = new DataStream(Width * Height * 4, true, true))
            {
                dataStream.WriteRange(textureData);
                dataStream.Position = 0;

                mImage = new Bitmap(InterfaceManager.Instance.Surface.RenderTarget, new Size2(Width, Height),
                                    new DataPointer(dataStream.DataPointer, Width * Height * 4), Width * 4, bmpProps);
            }
        }
Exemplo n.º 9
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Direct2D
            var renderTargetProperties = new RenderTargetProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)
            };
            var hwndRenderTargetProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = this.Handle,
                PixelSize      = new Size2(bounds.Width, bounds.Height),
                PresentOptions = PresentOptions.Immediately,
            };

            renderTarget = new WindowRenderTarget(factory, renderTargetProperties, hwndRenderTargetProperties);

            var bitmapProperties = new BitmapProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore)
            };

            bitmap = new SharpDX.Direct2D1.Bitmap(renderTarget, new Size2(bounds.Width, bounds.Height), bitmapProperties);

            textFormat = new TextFormat(directWriteFacrtory, "Arial", FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, 96.0f);
            textFormat.ParagraphAlignment = ParagraphAlignment.Center;
            textFormat.TextAlignment      = TextAlignment.Center;

            solidColorBrush = new SolidColorBrush(renderTarget, Color4.White);
        }
Exemplo n.º 10
0
        public static Bitmap ToSharpDXBitmap(RenderTarget renderTarget, System.Drawing.Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return(null);
            }

            BitmapData drawingBitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                           ImageLockMode.ReadOnly,
                                                           System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            DataStream dataStream = new DataStream(drawingBitmapData.Scan0,
                                                   drawingBitmapData.Stride * drawingBitmapData.Height,
                                                   true, false);

            BitmapProperties properties = new BitmapProperties()
            {
                PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
            };

            try
            {
                return(new Bitmap(renderTarget, new Size2(bitmap.Width, bitmap.Height), dataStream, drawingBitmapData.Stride, properties));
            }
            finally
            {
                bitmap.UnlockBits(drawingBitmapData);
            }
        }
Exemplo n.º 11
0
        public static Bitmap ToD2D1Bitmap(this SdBitmap bitmap, RenderTarget renderTarget, PixelFormat pixelFormat)
        {
            var sourceArea       = new SdRect(0, 0, bitmap.Width, bitmap.Height);
            var bitmapProperties = new BitmapProperties(pixelFormat);
            var size             = new SharpDX.Size2(bitmap.Width, bitmap.Height);

            // Transform pixels from BGRA to RGBA.
            var stride = bitmap.Width * sizeof(int);

            using (var tempStream = new SharpDX.DataStream(bitmap.Height * stride, true, true))
            {
                // Lock System.Drawing.Bitmap
                var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                // Convert all pixels.
                for (var y = 0; y < bitmap.Height; y++)
                {
                    var offset = bitmapData.Stride * y;
                    for (var x = 0; x < bitmap.Width; x++)
                    {
                        // Not optimized.
                        var B = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        var G = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        var R = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        var A = Marshal.ReadByte(bitmapData.Scan0, offset++);
                        tempStream.Write(R | (G << 8) | (B << 16) | (A << 24));
                    }
                }
                bitmap.UnlockBits(bitmapData);
                tempStream.Position = 0;
                return(new Bitmap(renderTarget, size, tempStream, stride, bitmapProperties));
            }
        }
Exemplo n.º 12
0
        public static SharpDX.Direct2D1.Bitmap LoadBitmapFromResourceName(this RenderTarget renderTarget, string resourceName)
        {
            System.Drawing.Bitmap bitmap2;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(typeof(MusicCanvasControl), resourceName);
            if (image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
            {
                bitmap2 = new System.Drawing.Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                using (Graphics graphics = Graphics.FromImage(bitmap2))
                {
                    graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                }
            }
            else
            {
                bitmap2 = image;
            }
            BitmapData bitmapdata = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
            int        length     = bitmapdata.Stride * bitmap2.Height;

            byte[] destination = new byte[length];
            Marshal.Copy(bitmapdata.Scan0, destination, 0, length);
            bitmap2.UnlockBits(bitmapdata);
            SharpDX.Direct2D1.PixelFormat pixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            BitmapProperties bitmapProperties         = new BitmapProperties(pixelFormat, bitmap2.HorizontalResolution, bitmap2.VerticalResolution);

            SharpDX.Direct2D1.Bitmap bitmap3 = new SharpDX.Direct2D1.Bitmap(renderTarget, new Size2(bitmap2.Width, bitmap2.Height), bitmapProperties);
            bitmap3.CopyFromMemory(destination, bitmapdata.Stride);
            bitmap2.Dispose();
            image.Dispose();
            return(bitmap3);
        }
Exemplo n.º 13
0
 public BitmapImage(int width, int height, BitmapProperties properties)
 {
     mWidth      = width;
     mHeight     = height;
     mProperties = properties;
     mData       = new DataStream(width * height * 4, true, true);
 }
Exemplo n.º 14
0
        public void InitRendering()
        {
            try
            {
                lock (_drawLock)
                {
                    m_Ready      = false;
                    ResizeRedraw = true;
                    var desc = new SwapChainDescription
                    {
                        BufferCount     = 2,
                        ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1),
                                                              Format.R8G8B8A8_UNorm),
                        IsWindowed        = true,
                        OutputHandle      = Handle,
                        SampleDescription = new SampleDescription(1, 0),
                        SwapEffect        = SwapEffect.Discard,
                        Usage             = Usage.RenderTargetOutput | Usage.Shared
                    };

                    Device.CreateWithSwapChain(DriverType.Hardware,
                                               DeviceCreationFlags.BgraSupport,
                                               new[] { SharpDX.Direct3D.FeatureLevel.Level_9_3 },
                                               desc,
                                               out device,
                                               out swapChain);

                    var d2dFactory = new SharpDX.Direct2D1.Factory();

                    Factory factory = swapChain.GetParent <Factory>();
                    factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);

                    Texture2D backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0);

                    surface = backBuffer.QueryInterface <Surface>();

                    d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface,
                                                                         new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));


                    var bitmapProperties = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore));

                    clientArea = new RawRectangleF
                    {
                        Left   = 0,
                        Top    = 0,
                        Bottom = ClientSize.Height,
                        Right  = ClientSize.Width
                    };

                    factory.Dispose();
                    backBuffer.Dispose();
                    m_Ready = true;
                }
            }
            catch (Exception ee)
            {
            }
        }
Exemplo n.º 15
0
 public RenderService()
 {
     bitmaps = new Dictionary<string, Bitmap>();
     brushes = new Dictionary<Tuple<Color4, float>, SolidColorBrush>();
     writeFactory = new SlimDX.DirectWrite.Factory(SlimDX.DirectWrite.FactoryType.Shared);
     clearColor = new Color4(1, 1, 1);
     PixelFormat pixelFormat = new PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
     bitmapProperties = new BitmapProperties() { PixelFormat = pixelFormat };
 }
Exemplo n.º 16
0
        public Bitmap CreateBitmapFromResource(string name)
        {
            var assembly = Assembly.GetExecutingAssembly();

            bool needsScaling = false;

            System.Drawing.Bitmap bmp;

            if (Direct2DTheme.MainWindowScaling == 1.5f && assembly.GetManifestResourceInfo($"FamiStudio.Resources.{name}@15x.png") != null)
            {
                bmp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}@15x.png")) as System.Drawing.Bitmap;
            }
            else if (Direct2DTheme.MainWindowScaling > 1.0f && assembly.GetManifestResourceInfo($"FamiStudio.Resources.{name}@2x.png") != null)
            {
                bmp          = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}@2x.png")) as System.Drawing.Bitmap;
                needsScaling = Direct2DTheme.MainWindowScaling != 2.0f;
            }
            else
            {
                bmp = System.Drawing.Image.FromStream(assembly.GetManifestResourceStream($"FamiStudio.Resources.{name}.png")) as System.Drawing.Bitmap;
            }

            // Pre-resize all images so we dont have to deal with scaling later.
            if (needsScaling)
            {
                var newWidth  = (int)(bmp.Width * (Direct2DTheme.MainWindowScaling / 2.0f));
                var newHeight = (int)(bmp.Height * (Direct2DTheme.MainWindowScaling / 2.0f));

                bmp = new System.Drawing.Bitmap(bmp, newWidth, newHeight);
            }

            var bmpData =
                bmp.LockBits(
                    new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            var stream   = new DataStream(bmpData.Scan0, bmpData.Stride * bmpData.Height, true, false);
            var pFormat  = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
            var bmpProps = new BitmapProperties(pFormat);

            var result =
                new Bitmap(
                    renderTarget,
                    new Size2(bmp.Width, bmp.Height),
                    stream,
                    bmpData.Stride,
                    bmpProps);

            bmp.UnlockBits(bmpData);

            stream.Dispose();
            bmp.Dispose();

            return(result);
        }
Exemplo n.º 17
0
        public void drawPixels(int[] pixels, int x, int y, int w, int h)
        {
            var bitmapProperties = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore));

            using (Bitmap gameBitmap = new Bitmap(d2dRenderTarget, new Size2(w, h), bitmapProperties))
            {
                gameBitmap.CopyFromMemory(pixels, w * 4);
                d2dRenderTarget.DrawBitmap(gameBitmap, clientArea, 1.0f, BitmapInterpolationMode.NearestNeighbor);
            }
        }
Exemplo n.º 18
0
        public unsafe Bitmap CreateBitmap(int width, int height, uint[] data)
        {
            fixed(uint *ptr = &data[0])
            {
                DataStream       stream   = new DataStream(new IntPtr(ptr), data.Length * sizeof(uint), true, false);
                BitmapProperties bmpProps = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
                var bmp = new Bitmap(renderTarget, new Size2(width, height), stream, width * sizeof(uint), bmpProps);

                stream.Dispose();
                return(bmp);
            }
        }
Exemplo n.º 19
0
        private static Bitmap LoadBitmap(SharpDX.Direct2D1.RenderTarget target, System.Drawing.Bitmap bitmap)
        {
            var format   = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
            var bmpProps = new BitmapProperties(format);
            var bmp      = new Bitmap(target, new Size2(bitmap.Width, bitmap.Height), bmpProps);

            var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            bmp.CopyFromMemory(bitmapData.Scan0, bitmapData.Stride);
            bitmap.UnlockBits(bitmapData);

            return(bmp);
        }
Exemplo n.º 20
0
        public void UpdateMap(string mapName)
        {
            mWdlFile = new IO.Files.Terrain.WdlFile();
            try
            {
                mWdlFile.Load(mapName);
                if (mWdlFile.HasEntries == false)
                {
                    UpdateMapNoWdl(mapName);
                    return;
                }
            }
            catch (Exception)
            {
                UpdateMapNoWdl(mapName);
                return;
            }

            if (mImage != null)
            {
                mImage.Dispose();
            }

            var textureData = new uint[Width * Height];

            for (var i = 0; i < 64; ++i)
            {
                for (var j = 0; j < 64; ++j)
                {
                    if (mWdlFile.HasEntry(j, i) == false)
                    {
                        continue;
                    }

                    var entry = mWdlFile.GetEntry(j, i);
                    LoadEntry(entry, textureData, ref i, ref j);
                }
            }

            var bmpProps =
                new BitmapProperties(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore));

            using (var dataStream = new DataStream(Width * Height * 4, true, true))
            {
                dataStream.WriteRange(textureData);
                dataStream.Position = 0;

                mImage = new Bitmap(InterfaceManager.Instance.Surface.RenderTarget, new Size2(Width, Height),
                                    new DataPointer(dataStream.DataPointer, Width * Height * 4), Width * 4, bmpProps);
            }
        }
Exemplo n.º 21
0
        static public Dot9BitmapD2D CreateDot9BoxShadowBitmap(D2DGraphics g, float cornersRadius, float shadowRadius, System.Drawing.Color shadowColor)
        {
            int width  = (int)(cornersRadius * 2 + shadowRadius * 4 + 2);
            int height = (int)(cornersRadius * 2 + shadowRadius * 4 + 2);

            SharpDX.Direct2D1.PixelFormat format = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
            BitmapProperties prop = new BitmapProperties(format);

            SharpDX.Direct2D1.Bitmap bitmap = new SharpDX.Direct2D1.Bitmap(g.RenderTarget, new Size2(width, height), prop);

            unsafe
            {
                System.Drawing.Bitmap     gdibmp = new System.Drawing.Bitmap(width, height);
                System.Drawing.RectangleF rect   = new System.Drawing.RectangleF(shadowRadius, shadowRadius, width - shadowRadius * 2, height - shadowRadius * 2);
                Graphics             gdiGraph    = Graphics.FromImage(gdibmp);
                GraphicsPath         myPath      = DrawUtils.CreateRoundedRectanglePath(rect, cornersRadius);
                System.Drawing.Brush brush       = new SolidBrush(shadowColor);
                gdiGraph.FillPath(brush, myPath);
                brush.Dispose();

                GaussianBlur gs = new GaussianBlur((int)shadowRadius);
                gdibmp = gs.ProcessImage(gdibmp);

                System.Drawing.Rectangle bitmapRect = new System.Drawing.Rectangle(0, 0, gdibmp.Width, gdibmp.Height);
                BitmapData srcBmData = gdibmp.LockBits(bitmapRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                byte *     ptr       = (byte *)srcBmData.Scan0;
                bitmap.CopyFromMemory((IntPtr)ptr, srcBmData.Stride);
                gdibmp.UnlockBits(srcBmData);

                //  gdibmp.Save("f:\\shadow.png");
                gdibmp.Dispose();
            }



            int[] widthPts = new int[]
            {
                (int)(cornersRadius + shadowRadius * 2 + 1), (int)(width - cornersRadius - shadowRadius * 2 - 1)
            };

            int[] heightPts = new int[]
            {
                (int)(cornersRadius + shadowRadius * 2 + 1), (int)(width - cornersRadius - shadowRadius * 2 - 1)
            };


            Dot9BitmapD2D dot9Bitmap = new Dot9BitmapD2D(bitmap, widthPts, heightPts);

            return(dot9Bitmap);
        }
Exemplo n.º 22
0
        public Bitmap CreateBitmap(DataStream stream)
        {
            var bitmapProperties = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm,
                                                                                          SharpDX.Direct2D1.AlphaMode.Premultiplied));

            if (_renderTarget2D == null)
            {
                return(null);
            }

            return(new SharpDX.Direct2D1.Bitmap(_renderTarget2D,
                                                new Size2((int)_viewPort.Width, (int)_viewPort.Height), stream,
                                                (int)_viewPort.Width * sizeof(int), bitmapProperties));
        }
Exemplo n.º 23
0
        public void loadFromFile(string file)
        {
            if (!File.Exists(file))
            {
                Console.WriteLine("Не найден файл " + file);
                throw new Exception("Не найден файл " + file);
            }
            using (var bitmap = (System.Drawing.Bitmap)Image.FromFile(file))
            {
                var sourceArea       = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bitmapProperties =
                    new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm,
                                                         AlphaMode.Premultiplied));
                var size = new Size(bitmap.Width, bitmap.Height);

                // Transform pixels from BGRA to RGBA
                int stride = bitmap.Width * sizeof(int);
                using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
                {
                    // Lock System.Drawing.Bitmap
                    var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly,
                                                     System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                    // Convert all pixels
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            // Not optimized
                            byte R    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte G    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte B    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte A    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            int  rgba = R | (G << 8) | (B << 16) | (A << 24);
                            tempStream.Write(rgba);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    _width              = bitmap.Width;
                    _height             = bitmap.Height;
                    tempStream.Position = 0;
                    GraphicCore core = GraphicCore.getInstance();
                    source = new SharpDX.Direct2D1.Bitmap(core.render2d, size, tempStream, stride, bitmapProperties);
                }
            }
            _isLoaded = true;
            dispatchEvent(new Event(this, Event.COMPLETE));
        }
Exemplo n.º 24
0
        private void CreateImageResource(RenderTarget pRenderer, byte[] pData, int pStride)
        {
            var properties = new BitmapProperties
            {
                PixelFormat = new PixelFormat(
                    Format.B8G8R8A8_UNORM,
                    AlphaMode.Premultiplied)
            };

            // Copy the RGB values into the array.
            //System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, bytes.Length);
            //Data = new MemoryStream();
            //Data.Write(bytes, 0, bytes.Length);
            //Stride = bmpData.Stride;
        }
Exemplo n.º 25
0
        /// <summary>
        /// This method loads a bitmap file into memory for us.
        /// </summary>
        /// <param name="filename"></param>
        public SlimDXBitmap LoadBitmap(string filename)
        {
            // This will hold the Direct2D Bitmap that we will return at the end of this function.
            SlimDXBitmap d2dBitmap = null;


            // Load the bitmap using the System.Drawing.Bitmap class.
            SystemBitmap originalImage = new SystemBitmap(filename);

            // Create a rectangle holding the size of the bitmap image.
            Rectangle bounds = new Rectangle(0, 0, originalImage.Width, originalImage.Height);

            // Lock the memory holding this bitmap so that only we are allowed to mess with it.
            BitmapData imageData = originalImage.LockBits(bounds, ImageLockMode.ReadOnly, SystemPixelFormat.Format32bppPArgb);

            // Create a DataStream attached to the bitmap.
            DataStream dataStream = new DataStream(imageData.Scan0,
                                                   imageData.Stride * imageData.Height,
                                                   true,
                                                   false);

            // Set the pixel format and properties.
            BitmapProperties bmpProperties = new BitmapProperties
            {
                PixelFormat = new SlimDXPixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)
            };


            // Copy the image data into a new SlimDX.Direct2D.Bitmap object.
            d2dBitmap = new SlimDXBitmap(
                this.m_RenderTarget,
                new Size(bounds.Width, bounds.Height),
                dataStream,
                imageData.Stride,
                bmpProperties);

            // Unlock the memory that is holding the original bitmap object.
            originalImage.UnlockBits(imageData);


            // Get rid of the original bitmap object since we no longer need it.
            originalImage.Dispose();


            // Return the Direct2D bitmap.
            return(d2dBitmap);
        }
Exemplo n.º 26
0
        public void Render()
        {
            d2dWindowRenderTarget.BeginDraw();
            d2dWindowRenderTarget.Clear(new Color4(Color.Black));
            int sizex, sizey;

            foreach (gameobject curr in ToDraw)
            {
                if (curr.Game_object_state == 0)
                {
                    sizex = targetControl.Width;
                    sizey = targetControl.Height;
                }
                else
                {
                    sizex = 200;
                    sizey = 200;
                }
                if (curr.D2dbm == null)
                {
                    d2dPixelFormat                  = new PixelFormat(SlimDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
                    d2dBitmapProperties             = new BitmapProperties();
                    d2dBitmapProperties.PixelFormat = d2dPixelFormat;

                    bitmapData = curr.Game_object_bitmap.LockBits(new Rectangle(new Point(0, 0), new Size(curr.Game_object_bitmap.Width, curr.Game_object_bitmap.Height)), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                    dataStream = new DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
                    SlimDX.Direct2D.Bitmap temp = new SlimDX.Direct2D.Bitmap(d2dWindowRenderTarget, new Size(curr.Game_object_bitmap.Width, curr.Game_object_bitmap.Height), dataStream, bitmapData.Stride, d2dBitmapProperties);
                    curr.D2dbm = temp;
                    curr.Game_object_bitmap.UnlockBits(bitmapData);
                    d2dWindowRenderTarget.DrawBitmap(curr.D2dbm);
                    dataStream.Dispose();
                }
                else
                {
                    //d2dWindowRenderTarget.DrawBitmap(curr.D2dbm, new Rectangle(curr.Game_object_position.X, curr.Game_object_position.Y, targetControl.Width, targetControl.Height));
                    d2dWindowRenderTarget.DrawBitmap(curr.D2dbm, new Rectangle(curr.Game_object_position.X, curr.Game_object_position.Y, sizex, sizey), 1.0f, InterpolationMode.Linear, new Rectangle(1024 * curr.Game_object_state, 0, 1024, curr.Game_object_bitmap.Height));
                }
            }



            d2dWindowRenderTarget.DrawRectangle(new SolidColorBrush(d2dWindowRenderTarget, new Color4(Color.Red)), new Rectangle(20, 20, targetControl.Width - 40, targetControl.Height - 40));
            d2dWindowRenderTarget.DrawText(Form1.counter.ToString(), new TextFormat(wrtFactory, "Arial", SlimDX.DirectWrite.FontWeight.Normal, SlimDX.DirectWrite.FontStyle.Normal, FontStretch.Normal, 18, "en-us"), new Rectangle(new Point(0, 0), new Size(200, 20)), new SolidColorBrush(d2dWindowRenderTarget, new Color4(Color.Blue)));
            d2dWindowRenderTarget.EndDraw();
            Form1.tempcounter++;
        }
Exemplo n.º 27
0
        public Bitmap LoadBitmap(string pFile)
        {
            System.Diagnostics.Debug.Assert(System.IO.File.Exists(pFile));

            // Loads from file using System.Drawing.Image
            using (var bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(pFile))
            {
                var width            = bitmap.Width;
                var height           = bitmap.Height;
                var sourceArea       = new System.Drawing.Rectangle(0, 0, width, height);
                var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied));
                var size             = new Size2(width, height);

                // Transform pixels from BGRA to RGBA
                int stride = width * sizeof(int);
                using (var tempStream = new DataStream(height * stride, true, true))
                {
                    // Lock System.Drawing.Bitmap
                    var bitmapData = bitmap.LockBits(sourceArea, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                    // Convert all pixels
                    for (int y = 0; y < height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < width; x++)
                        {
                            //Convert BGRA to RGBA
                            int bgra = System.Runtime.InteropServices.Marshal.ReadInt32(bitmapData.Scan0, offset);
                            offset += 4;

                            byte B    = (byte)bgra;
                            byte G    = (byte)(bgra >> 8);
                            byte R    = (byte)(bgra >> 16);
                            byte A    = (byte)(bgra >> 24);
                            int  rgba = R | (G << 8) | (B << 16) | (A << 24);
                            tempStream.Write(rgba);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;

                    return(new Bitmap(Context, size, tempStream, stride, bitmapProperties));
                }
            }
        }
Exemplo n.º 28
0
        private ID2D1Bitmap LoadD2D1BitmapFromBitmap(ID2D1RenderTarget renderTarget, Bitmap origin)
        {
            Bitmap destBitmap;


            if (origin.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
            {
                destBitmap = new Bitmap(origin.Width, origin.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                using (Graphics g = Graphics.FromImage(destBitmap))
                {
                    g.DrawImage(origin, 0, 0);
                    g.Flush();
                }
            }
            else
            {
                destBitmap = (Bitmap)origin.Clone();
            }

            var bmpData = destBitmap.LockBits(new Rectangle(0, 0, destBitmap.Width, destBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, destBitmap.PixelFormat);

            int buffSize = bmpData.Stride * destBitmap.Height;

            byte[] byteData = new byte[buffSize];

            Marshal.Copy(bmpData.Scan0, byteData, 0, buffSize);

            destBitmap.UnlockBits(bmpData);


            var bmpProps = new BitmapProperties
            {
                PixelFormat = new Vortice.DCommon.PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                DpiX        = 96,
                DpiY        = 96
            };

            var d2d1Bmp = renderTarget.CreateBitmap(new Vortice.Mathematics.Size(destBitmap.Width, destBitmap.Height), IntPtr.Zero, bmpData.Stride, bmpProps);

            d2d1Bmp.CopyFromMemory(byteData, bmpData.Stride);

            return(d2d1Bmp);
        }
        public NinePartsBitmap(RenderTarget target, Bitmap source, int left, int right, int top, int bottom)
        {
            BitmapProperties bitmapProperties = new BitmapProperties()
            {
                PixelFormat = new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied),
            };

            Size2 sourceSize = source.PixelSize;

            _topLeftCornerSize     = new Size2(left, top);
            _topRightCornerSize    = new Size2(sourceSize.Width - right, top);
            _bottomLeftCornerSize  = new Size2(left, sourceSize.Height - bottom);
            _bottomRightCornerSize = new Size2(sourceSize.Width - right, sourceSize.Height - bottom);

            _leftSideSize   = new Size2(left, bottom - top);
            _rightSideSize  = new Size2(sourceSize.Width - right, bottom - top);
            _topSideSize    = new Size2(right - left, top);
            _bottomSideSize = new Size2(right - left, sourceSize.Height - bottom);

            _centerSize = new Size2(right - left, bottom - top);

            _topLeftCorner     = new Bitmap(target, _topLeftCornerSize, bitmapProperties);
            _topRightCorner    = new Bitmap(target, _topRightCornerSize, bitmapProperties);
            _bottomLeftCorner  = new Bitmap(target, _bottomLeftCornerSize, bitmapProperties);
            _bottomRightCorner = new Bitmap(target, _bottomRightCornerSize, bitmapProperties);
            _leftSide          = new Bitmap(target, _leftSideSize, bitmapProperties);
            _rightSide         = new Bitmap(target, _rightSideSize, bitmapProperties);
            _topSide           = new Bitmap(target, _topSideSize, bitmapProperties);
            _bottomSide        = new Bitmap(target, _bottomSideSize, bitmapProperties);
            _center            = new Bitmap(target, _centerSize, bitmapProperties);

            _topLeftCorner.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(0, 0, left, top));
            _topRightCorner.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(right, 0, sourceSize.Width, top));
            _bottomLeftCorner.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(0, bottom, left, sourceSize.Height));
            _bottomRightCorner.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(right, bottom, sourceSize.Width, sourceSize.Height));

            _leftSide.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(0, top, left, bottom));
            _rightSide.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(right, top, sourceSize.Width, bottom));
            _topSide.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(left, 0, right, top));
            _bottomSide.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(left, bottom, right, sourceSize.Height));

            _center.CopyFromBitmap(source, new RawPoint(0, 0), new RawRectangle(left, top, right, bottom));
        }
Exemplo n.º 30
0
        public void Draw()
        {
            if (this._d2dbitmap == null)
            {
                // Load the texture
                var bitmapData = this.tileSheetBitmap.LockBits(
                    tileRect,
                    System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    System.Drawing.Imaging.PixelFormat.Format32bppPArgb
                );
                var dataStream = new DataStream(
                    bitmapData.Scan0,
                    bitmapData.Stride * bitmapData.Height,
                    true,
                    false
                );
                var d2dPixelFormat = new PixelFormat(
                    SlimDX.DXGI.Format.B8G8R8A8_UNorm,
                    SlimDX.Direct2D.AlphaMode.Premultiplied
                );
                var d2dBitmapProperties = new BitmapProperties();
                d2dBitmapProperties.PixelFormat = d2dPixelFormat;

                _d2dbitmap = new SlimDX.Direct2D.Bitmap(
                    _device.RenderTarget,
                    tileRect.Size,
                    dataStream,
                    bitmapData.Stride,
                    d2dBitmapProperties
                );
                this.tileSheetBitmap.UnlockBits(bitmapData);
            }

            // Render to the 2D context
            _device.RenderTarget.DrawBitmap(
                _d2dbitmap,
                new Rectangle(
                    this.drawToPosition,
                    new Size(this.tileRect.Width, this.tileRect.Height)
                )
            );
        }
Exemplo n.º 31
0
        public Bitmap LoadBitmap(string file)
        {
            // Loads from file using System.Drawing.Image
            using (var bitmap = (System.Drawing.Bitmap)Image.FromFile(file))
            {
                var sourceArea       = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bitmapProperties =
                    new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
                var size = new Size2(bitmap.Width, bitmap.Height);

                // Transform pixels from BGRA to RGBA
                var stride = bitmap.Width * sizeof(int);
                using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
                {
                    // Lock System.Drawing.Bitmap
                    var bitmapData = bitmap.LockBits(
                        sourceArea,
                        ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                    // Convert all pixels
                    for (var y = 0; y < bitmap.Height; y++)
                    {
                        var offset = bitmapData.Stride * y;
                        for (var x = 0; x < bitmap.Width; x++)
                        {
                            // Not optimized
                            var B    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            var G    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            var R    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            var A    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            var rgba = R | (G << 8) | (B << 16) | (A << 24);
                            tempStream.Write(rgba);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;

                    return(new Bitmap(this.device, size, tempStream, stride, bitmapProperties));
                }
            }
        }
Exemplo n.º 32
0
        public static SlimDX.Direct2D.Bitmap CreateBitmapFromWicBitmap(IWICBitmapSource source, BitmapProperties bitmapProperties, RenderTarget renderTarget)
        {
            var pBitmap = IntPtr.Zero;
            SlimDX.Direct2D.Bitmap bmp = null;

            var pRenderTarget = Marshal.GetObjectForIUnknown(renderTarget.ComPointer) as ID2D1RenderTarget;

            int hr = pRenderTarget.CreateBitmapFromWicBitmap(source, ref bitmapProperties, out pBitmap);

            if (hr != 0)
                goto cleanup;
          
            bmp = SlimDX.Direct2D.Bitmap.FromPointer(pBitmap);
           
cleanup:
            Marshal.Release(pBitmap);
            Marshal.ReleaseComObject(pRenderTarget);

            return bmp;
        }
Exemplo n.º 33
0
        private void CreateBitmap()
        {
            BitmapData bitmapData = this.orgBitmap.LockBits(new Rectangle(0, 0, this.orgBitmap.Width, this.orgBitmap.Height),
                                                            ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

            using (DataStream dataStream = new DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false))
            {
                PixelFormat      format     = new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied);
                BitmapProperties properties = new BitmapProperties();
                properties.HorizontalDpi = properties.VerticalDpi = 96;
                properties.PixelFormat   = format;
                if (this.SpriteBitmap != null && !this.SpriteBitmap.Disposed)
                {
                    this.SpriteBitmap.Dispose();
                }
                this.SpriteBitmap = new SlimDX.Direct2D.Bitmap(this.batch.DWRenderTarget, new Size(this.orgBitmap.Width, this.orgBitmap.Height),
                                                               dataStream, bitmapData.Stride, properties);
                this.orgBitmap.UnlockBits(bitmapData);
            }
        }
Exemplo n.º 34
0
        private void InitializeResources(Surface surface)
        {
            m_renderTargetProperties = new RenderTargetProperties();

            m_renderTargetProperties.HorizontalDpi = 96;
            m_renderTargetProperties.VerticalDpi = 96;
            m_renderTargetProperties.PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied);
            m_renderTargetProperties.Usage = RenderTargetUsage.None;
            m_renderTargetProperties.MinimumFeatureLevel = FeatureLevel.Direct3D10;
            
            InternalRenderTarget = RenderTarget.FromDXGI(m_deviceContext10.Direct2DFactory, 
                                                         surface,
                                                         m_renderTargetProperties);

            InternalRenderTarget.AntialiasMode = AntialiasMode.PerPrimitive;

            var bitmapProperties = new BitmapProperties();
            bitmapProperties.PixelFormat = new PixelFormat(m_format, AlphaMode.Premultiplied);

            InternalBitmap = new Bitmap(InternalRenderTarget, surface, bitmapProperties);
        }
Exemplo n.º 35
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.º 36
0
        private void OnNewAllocatorSurfaceCallback(object sender, IntPtr pSurface, IntPtr sharedHandle, Misc.Size size)
        {
            /* Reset our fields */
            IsVideoReady = false;
            NaturalSize = new Size(0, 0);

            /* Free any resources that may be in use */
            if(m_internalBitmap != null)
            {
                m_internalBitmap.Dispose();
                m_internalBitmap = null;
            }

            if(m_sharedSurface != null)
            {
                m_sharedSurface.Dispose();
                m_sharedSurface = null;
            }

            Texture2D sharedTexture2D = null;
            var device = m_directCanvasFactory.DeviceContext.Device;

            if (sharedHandle == IntPtr.Zero)
                return;

            try
            {
                /* Open up the shared surface using the passed shared handle */
                sharedTexture2D = device.OpenSharedResource<Texture2D>(sharedHandle);
            }
            catch (Exception)
            {
            }
            
            if(sharedTexture2D == null)
            {
                return;
            }

            /* Wrap our Texture2D in our Texture class */
            m_sharedSurface = new Texture(sharedTexture2D);


            var props = new BitmapProperties();
            props.PixelFormat = new PixelFormat(Format.Unknown, 
                                                AlphaMode.Premultiplied);

            /* Create a Direct2D bitmap, using the shared surface provided by 
             * our custom allocator */
            m_internalBitmap = new Bitmap(ResourceOwner.InternalRenderTarget, 
                                          m_sharedSurface.InternalDxgiSurface, 
                                          props);

            NaturalSize = new Size(m_sharedSurface.Description.Width, 
                                   m_sharedSurface.Description.Height);
            IsVideoReady = true;
        }