Example #1
0
    internal Direct2DTexture2D(IRenderTarget InRenderTarget, Bitmap InBitmap)
    {
        Direct2DResourceManager.OnResourceCreate(this);
        Direct2DRenderTarget RT = InRenderTarget as Direct2DRenderTarget;

        Texture2D = CreateFromBitmap(RT.RenderTarget, InBitmap);
    }
Example #2
0
        public void DrawImage(Object image, System.Drawing.RectangleF srcRect, System.Drawing.RectangleF destRect)
        {
            if (image is SharpDX.Direct2D1.Bitmap)
            {
                SharpDX.Direct2D1.Bitmap bImg = (SharpDX.Direct2D1.Bitmap)image;
                if (!bImg.IsDisposed)
                {
                    SharpDX.Mathematics.Interop.RawRectangleF src = new SharpDX.Mathematics.Interop.RawRectangleF
                    {
                        Top    = srcRect.Top,
                        Left   = srcRect.Left,
                        Bottom = srcRect.Bottom,
                        Right  = srcRect.Width
                    };

                    SharpDX.Mathematics.Interop.RawRectangleF dest = new SharpDX.Mathematics.Interop.RawRectangleF
                    {
                        Top    = destRect.Top,
                        Left   = destRect.Left,
                        Bottom = destRect.Bottom,
                        Right  = destRect.Right
                    };

                    d2dRenderTarget.DrawBitmap(bImg, dest, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear, src);
                }        // Endif image is not disposed
            }            // Endif image is DirectX Bitmap
        }
Example #3
0
 public void Dispose()
 {
     Direct2DResourceManager.OnResourceDestroy(this);
     if (Texture2D != null)
     {
         Texture2D.Dispose();
         Texture2D = null;
     }
     if (fConverter != null)
     {
         fConverter.Dispose();
         fConverter = null;
     }
     if (BFDecorde != null)
     {
         BFDecorde.Dispose();
         BFDecorde = null;
     }
     if (BitDecorder != null)
     {
         BitDecorder.Dispose();
         BitDecorder = null;
     }
     if (MemStream != null)
     {
         MemStream.Dispose();
         MemStream = null;
     }
 }
Example #4
0
        public Object LoadImage(String filename)
        {
            Object image = null;

            if (File.Exists(filename))
            {
                using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(filename))
                {
                    System.Drawing.Imaging.BitmapData bData = bmp.LockBits(
                        new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb
                        );

                    SharpDX.DataStream                 stream  = new SharpDX.DataStream(bData.Scan0, bData.Stride * bData.Height, true, false);
                    SharpDX.Direct2D1.PixelFormat      pFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
                    SharpDX.Direct2D1.BitmapProperties bProps  = new SharpDX.Direct2D1.BitmapProperties(pFormat);

                    image = new SharpDX.Direct2D1.Bitmap(d2dRenderTarget, new Size2(bmp.Width, bmp.Height), stream, bData.Stride, bProps);
                    bmp.UnlockBits(bData);
                    stream.Dispose();
                }
            }

            return(image);
        }
Example #5
0
        private Bitmap CreateBitmap(string path)
        {
            var renderTarget   = renderTargetProvider.RenderTarget;
            var imagingFactory = imagingFactoryProvider.ImagingFactory;

            if (renderTarget == null || renderTarget.IsDisposed || imagingFactory == null || imagingFactory.IsDisposed)
            {
                return(null); // TODO: throw exception
            }

            var fileStream    = new NativeFileStream(path, NativeFileMode.Open, NativeFileAccess.Read);
            var bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand);
            var frame         = bitmapDecoder.GetFrame(0);

            var converter = new FormatConverter(imagingFactory);

            converter.Initialize(frame, PixelFormat.Format32bppPRGBA);

            var bitmap = Bitmap.FromWicBitmap(renderTarget, converter);

            Utilities.Dispose(ref bitmapDecoder);
            Utilities.Dispose(ref fileStream);

            return(bitmap);
        }
Example #6
0
        protected override void OnRenderInitialized(RenderGraphics g)
        {
            theme = RenderTheme.CreateResourcesForGraphics(g);

            bmpSong = g.CreateBitmapFromResource("Music");
            bmpInstrument[Project.ExpansionNone] = g.CreateBitmapFromResource("Instrument");
            bmpInstrument[Project.ExpansionVrc6] = g.CreateBitmapFromResource("InstrumentVRC6");
#if DEV
            bmpInstrument[Project.ExpansionVrc7]    = g.CreateBitmapFromResource("InstrumentVRC6");
            bmpInstrument[Project.ExpansionFds]     = g.CreateBitmapFromResource("Instrument");
            bmpInstrument[Project.ExpansionMmc5]    = g.CreateBitmapFromResource("Instrument");
            bmpInstrument[Project.ExpansionNamco]   = g.CreateBitmapFromResource("Instrument");
            bmpInstrument[Project.ExpansionSunsoft] = g.CreateBitmapFromResource("Instrument");
#endif
            bmpAdd            = g.CreateBitmapFromResource("Add");
            bmpDPCM           = g.CreateBitmapFromResource("DPCM");
            bmpDuty[0]        = g.CreateBitmapFromResource("Duty0");
            bmpDuty[1]        = g.CreateBitmapFromResource("Duty1");
            bmpDuty[2]        = g.CreateBitmapFromResource("Duty2");
            bmpDuty[3]        = g.CreateBitmapFromResource("Duty3");
            bmpDuty[4]        = g.CreateBitmapFromResource("Duty4");
            bmpDuty[5]        = g.CreateBitmapFromResource("Duty5");
            bmpDuty[6]        = g.CreateBitmapFromResource("Duty6");
            bmpDuty[7]        = g.CreateBitmapFromResource("Duty7");
            bmpArpeggio       = g.CreateBitmapFromResource("Arpeggio");
            bmpPitch          = g.CreateBitmapFromResource("Pitch");
            bmpVolume         = g.CreateBitmapFromResource("Volume");
            bmpLoadInstrument = g.CreateBitmapFromResource("InstrumentOpen");
        }
Example #7
0
        public bool LoadFromStream(string textureKey, Stream stream)
        {
            try
            {
                if (this.textureCache.ContainsKey(textureKey))
                {
                    return(true);
                }

                using (var bitmapDecoder = new BitmapDecoder(this.imagingFactory, stream, DecodeOptions.CacheOnDemand))
                {
                    var frame = bitmapDecoder.GetFrame(0);
                    using (var converter = new FormatConverter(this.imagingFactory))
                    {
                        converter.Initialize(frame, PixelFormat.Format32bppPRGBA); // Format32bppPRGBA
                        var bitmap = Bitmap.FromWicBitmap(this.renderContext.RenderTarget, converter);
                        this.textureCache[textureKey] = new D3D11Texture(bitmap);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(false);
            }
        }
Example #8
0
        /// <summary>
        /// Gets a Direct2D bitmap to use on the specified render target.
        /// </summary>
        /// <param name="renderTarget">The render target.</param>
        /// <returns>The Direct2D bitmap.</returns>
        public override OptionalDispose <D2DBitmap> GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget)
        {
            FormatConverter converter = new FormatConverter(Direct2D1Platform.ImagingFactory);

            converter.Initialize(WicImpl, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
            return(new OptionalDispose <D2DBitmap>(D2DBitmap.FromWicBitmap(renderTarget, converter), true));
        }
Example #9
0
        public void DrawBitmap(Bitmap bmp, int x, int y)
        {
            IbRect src = new IbRect(0, 0, bmp.PixelSize.Width, bmp.PixelSize.Height);
            IbRect dst = new IbRect(x + tbXloc, y + tbYloc - gv.oYshift, bmp.PixelSize.Width, bmp.PixelSize.Height);

            gv.DrawBitmap(bmp, src, dst);
        }
Example #10
0
 public void ResetLogBoxUiBitmaps()
 {
     btn_up     = gv.cc.LoadBitmap("btn_up.png");
     btn_down   = gv.cc.LoadBitmap("btn_down.png");
     btn_scroll = gv.cc.LoadBitmap("btn_scroll.png");
     bg_scroll  = gv.cc.LoadBitmap("bg_scroll.png");
 }
Example #11
0
        protected override void OnRenderInitialized(RenderGraphics g)
        {
            theme = RenderTheme.CreateResourcesForGraphics(g);

            bmpTracks[Channel.Square1]  = g.CreateBitmapFromResource("Square");
            bmpTracks[Channel.Square2]  = g.CreateBitmapFromResource("Square");
            bmpTracks[Channel.Triangle] = g.CreateBitmapFromResource("Triangle");
            bmpTracks[Channel.Noise]    = g.CreateBitmapFromResource("Noise");
            bmpTracks[Channel.DPCM]     = g.CreateBitmapFromResource("DPCM");

            bmpGhostNote = g.CreateBitmapFromResource("GhostSmall");

            seekBarBrush                  = g.CreateSolidBrush(ThemeBase.SeekBarColor);
            whiteKeyBrush                 = g.CreateHorizontalGradientBrush(0, trackNameSizeX, ThemeBase.LightGreyFillColor1, ThemeBase.LightGreyFillColor2);
            patternHeaderBrush            = g.CreateVerticalGradientBrush(0, patternHeaderSizeY, ThemeBase.LightGreyFillColor1, ThemeBase.LightGreyFillColor2);
            selectedPatternVisibleBrush   = g.CreateSolidBrush(Color.FromArgb(64, ThemeBase.LightGreyFillColor1));
            selectedPatternInvisibleBrush = g.CreateSolidBrush(Color.FromArgb(32, ThemeBase.LightGreyFillColor1));

            seekGeometry = g.CreateConvexPath(new[]
            {
                new Point(-headerSizeY / 2, 1),
                new Point(0, headerSizeY - 2),
                new Point(headerSizeY / 2, 1)
            });
        }
Example #12
0
 public override void DisposeDx()
 {
     if (this.dxBitmap != null)
     {
         this.dxBitmap.Dispose();
         this.dxBitmap = null;
     }
 }
Example #13
0
 internal D2dBitmap(D2dGraphics owner, SharpDX.Direct2D1.Bitmap bmp)
 {
     m_nativeBitmap             = bmp;
     m_owner                    = owner;
     m_bitmap                   = null;
     m_owner.RecreateResources += RecreateResources;
     m_rtNumber                 = owner.RenderTargetNumber;
 }
Example #14
0
 public void setupIbbHtmlLogBox(GameView g)
 {
     gv         = g;
     btn_up     = gv.cc.LoadBitmap("btn_up.png");
     btn_down   = gv.cc.LoadBitmap("btn_down.png");
     btn_scroll = gv.cc.LoadBitmap("btn_scroll.png");
     bg_scroll  = gv.cc.LoadBitmap("bg_scroll.png");
 }
Example #15
0
    internal Direct2DTexture2D(IRenderTarget InRenderTarget, string InFilePath)
    {
        Direct2DResourceManager.OnResourceCreate(this);
        Direct2DRenderTarget RT = InRenderTarget as Direct2DRenderTarget;
        var bitmap = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(InFilePath);

        Texture2D = CreateFromBitmap(RT.RenderTarget, bitmap);
    }
        private bool CreateOverlay()
        {
            if (_isOverlayInitialized && !_isOverlayReady && _gameMemory.Process.WindowHandle != IntPtr.Zero)
            {
                _window.Create();

                _graphics.Width        = _window.Width;
                _graphics.Height       = _window.Height;
                _graphics.WindowHandle = _window.Handle;
                _graphics.Setup();

                _window.SizeChanged += (object sender, OverlaySizeEventArgs e) =>
                                       _graphics.Resize(_window.Width, _window.Height);

                _window.FitTo(_gameMemory.Process.WindowHandle, true);

                if (_windowEventDispatcher != null)
                {
                    _windowEventDispatcher.Invoke(delegate
                    {
                        WinEventHook.WinEventDelegate windowEventDelegate = new WinEventHook.WinEventDelegate(MoveGameWindowEventCallback);
                        _windowEventGCHandle = GCHandle.Alloc(windowEventDelegate);
                        _windowEventHook     = WinEventHook.WinEventHookOne(WinEventHook.SWEH_Events.EVENT_OBJECT_LOCATIONCHANGE,
                                                                            windowEventDelegate,
                                                                            (uint)_gameMemory.Process.Id,
                                                                            WinEventHook.GetWindowThread(_gameMemory.Process.WindowHandle));
                    });
                }

                //Get a refernence to the underlying RenderTarget from SharpDX. This'll be used to draw portions of images.
                _device = (SharpDX.Direct2D1.WindowRenderTarget) typeof(Graphics)
                          .GetField("_device", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                          .GetValue(_graphics);

                _consolas14Bold = _graphics.CreateFont("Consolas", 14, true);
                _consolas16Bold = _graphics.CreateFont("Consolas", 16, true);
                _consolas32Bold = _graphics.CreateFont("Consolas", 32, true);

                _black      = _graphics.CreateSolidBrush(0, 0, 0, Config.Opacity);
                _white      = _graphics.CreateSolidBrush(255, 255, 255, Config.Opacity);
                _green      = _graphics.CreateSolidBrush(0, 128, 0, Config.Opacity);
                _lawngreen  = _graphics.CreateSolidBrush(124, 252, 0, Config.Opacity);
                _red        = _graphics.CreateSolidBrush(255, 0, 0, Config.Opacity);
                _darkred    = _graphics.CreateSolidBrush(139, 0, 0, Config.Opacity);
                _grey       = _graphics.CreateSolidBrush(128, 128, 128, Config.Opacity);
                _darkergrey = _graphics.CreateSolidBrush(60, 60, 60, Config.Opacity);
                _gold       = _graphics.CreateSolidBrush(255, 215, 0, Config.Opacity);
                _goldenrod  = _graphics.CreateSolidBrush(218, 165, 32, Config.Opacity);
                _violet     = _graphics.CreateSolidBrush(238, 130, 238, Config.Opacity);

                _characterSheet = ImageLoader.LoadBitmap(_device, Properties.Resources.portraits);
                _inventorySheet = ImageLoader.LoadBitmap(_device, Properties.Resources.objects);

                _isOverlayReady = true;
            }

            return(_isOverlayReady);
        }
Example #17
0
        /// <summary>
        /// Creates new instance of <see cref="Texture"/>
        /// </summary>
        /// <param name="data">Byte array that conatins texture information</param>
        public Texture(byte[] data)
        {
            RawData = data;
            BitmapSource src = GetBitmapSource(RawData);

            Bitmap = D2DBitmap.FromWicBitmap(Game.Renderer.RenderTarget, src);
            Height = (int)Bitmap.Size.Height;
            Width  = (int)Bitmap.Size.Width;
        }
Example #18
0
 public void Dispose()
 {
     Direct2DResourceManager.OnResourceDestroy(this);
     if (Texture2D != null)
     {
         Texture2D.Dispose();
         Texture2D = null;
     }
 }
Example #19
0
 public void journalCleanup()
 {
     //gv.screenType = "main";
     journalBack      = null;
     btnReturnJournal = null;
     ctrlUpArrow      = null;
     ctrlDownArrow    = null;
     ctrlLeftArrow    = null;
     ctrlRightArrow   = null;
 }
Example #20
0
        public void DrawBitmap(SharpDX.Direct2D1.Bitmap bmp, int x, int y)
        {
            //Rectangle src = new Rectangle(0, 0, bmp.Width, bmp.Height);
            //Rectangle dst = new Rectangle(x + tbXloc, y + tbYloc + gv.oYshift, bmp.Width, bmp.Height);
            //g.DrawImage(bmp, dst, src, GraphicsUnit.Pixel);
            IbRect src = new IbRect(0, 0, bmp.PixelSize.Width, bmp.PixelSize.Height);
            IbRect dst = new IbRect(x + tbXloc, y + tbYloc, bmp.PixelSize.Width, bmp.PixelSize.Height);

            gv.DrawBitmap(bmp, src, dst);
        }
Example #21
0
        public SharpDX.Direct2D1.Bitmap GetD2DBitmap(SharpDX.Direct2D1.DeviceContext dc)
        {
            BitmapSource src = Frame;

            // PixelFormat settings/conversion
            if (src.Format != System.Windows.Media.PixelFormats.Bgra32)
            {
                // Convert BitmapSource
                FormatConvertedBitmap fcb = new FormatConvertedBitmap();
                fcb.BeginInit();
                fcb.Source            = src;
                fcb.DestinationFormat = PixelFormats.Bgra32;
                fcb.EndInit();
                src = fcb;
            }

            if (src.PixelHeight > _maxImageSize || src.PixelWidth > _maxImageSize)
            {
                double            scale = (src.PixelWidth > src.PixelHeight) ? _maxImageSize / src.PixelWidth : _maxImageSize / src.PixelHeight;
                TransformedBitmap tb    = new TransformedBitmap();
                tb.BeginInit();
                tb.Source    = src;
                tb.Transform = new ScaleTransform(scale, scale);
                tb.EndInit();
                src = tb;
            }

            SharpDX.Direct2D1.Bitmap retval = null;
            GCHandle pinnedArray            = GCHandle.Alloc(null);

            try
            {
                int    stride     = src.PixelWidth * (src.Format.BitsPerPixel + 7) / 8;
                int    bufferSize = stride * src.PixelHeight;
                byte[] buffer     = new byte[bufferSize];
                src.CopyPixels(System.Windows.Int32Rect.Empty, buffer, stride, 0);
                pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                using (SharpDX.DataStream datastream = new SharpDX.DataStream(pinnedArray.AddrOfPinnedObject(), bufferSize, true, false))
                {
                    var bmpProps1 = new SharpDX.Direct2D1.BitmapProperties1(dc.PixelFormat, dc.Factory.DesktopDpi.Width, dc.Factory.DesktopDpi.Height);
                    retval = new SharpDX.Direct2D1.Bitmap1(dc, new SharpDX.Size2(src.PixelWidth, src.PixelHeight), datastream, stride, bmpProps1);
                }
            }
            catch
            {
            }
            finally
            {
                if (pinnedArray.IsAllocated)
                {
                    pinnedArray.Free();
                }
            }
            return(retval);
        }
Example #22
0
        /// <summary>
        /// Gets a Direct2D bitmap to use on the specified render target.
        /// </summary>
        /// <param name="renderTarget">The render target.</param>
        /// <returns>The Direct2D bitmap.</returns>
        public override SharpDX.Direct2D1.Bitmap GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget)
        {
            if (_direct2D == null)
            {
                FormatConverter converter = new FormatConverter(_factory);
                converter.Initialize(WicImpl, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
                _direct2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTarget, converter);
            }

            return(_direct2D);
        }
Example #23
0
        public SharpDX.Direct2D1.Bitmap GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget)
        {
            if (this.direct2D == null)
            {
                FormatConverter converter = new FormatConverter(this.factory);
                converter.Initialize(this.WicImpl, PixelFormat.Format32bppPBGRA);
                this.direct2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTarget, converter);
            }

            return this.direct2D;
        }
Example #24
0
        public SharpDX.Direct2D1.Bitmap GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget)
        {
            if (this.direct2D == null)
            {
                FormatConverter converter = new FormatConverter(this.factory);
                converter.Initialize(this.WicImpl, PixelFormat.Format32bppPBGRA);
                this.direct2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTarget, converter);
            }

            return(this.direct2D);
        }
Example #25
0
 private void CreateFromBitmap(SharpDX.Direct2D1.RenderTarget InRenderTarget, Bitmap InBitmap)
 {
     MemStream = new System.IO.MemoryStream();
     InBitmap.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
     BitDecorder = new SharpDX.WIC.BitmapDecoder(Direct2DDrawingSystem.instance.ImagingFactory,
                                                 MemStream,
                                                 SharpDX.WIC.DecodeOptions.CacheOnDemand);
     BFDecorde  = BitDecorder.GetFrame(0);
     fConverter = new SharpDX.WIC.FormatConverter(Direct2DDrawingSystem.instance.ImagingFactory);
     fConverter.Initialize(BFDecorde, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);
     Texture2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(InRenderTarget, fConverter);
 }
Example #26
0
        public void Render(RenderController renderController)
        {
            var rc_context = renderController.GetDeviceContext();

            try
            {
                SharpDX.DXGI.Resource           screenResource;
                OutputDuplicateFrameInformation frameInfo;

                duplicatedOutput.AcquireNextFrame(1000, out frameInfo, out screenResource);

                //Console.WriteLine(frameInfo.AccumulatedFrames);
                if (screenResource != null)
                {
                    using (var screenTexture2D = screenResource.QueryInterface <Texture2D>())
                    {
                        device.ImmediateContext.CopySubresourceRegion(
                            screenTexture2D,
                            0,
                            new ResourceRegion(_CaptureLeft, _CaptureTop, 0, _CaptureRight, _CaptureBottom, 1),
                            captureTexture,
                            0
                            );
                    }
                    var mapSource = device.ImmediateContext.MapSubresource(captureTexture, 0, MapMode.Read, MapFlags.None);
                    var outbmp    = new SharpDX.Direct2D1.Bitmap(
                        rc_context,
                        new Size2(_CaptureWidth, _CaptureHeight),
                        new SharpDX.Direct2D1.BitmapProperties(
                            new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                              SharpDX.Direct2D1.AlphaMode.Ignore)
                            )
                        );
                    outbmp.CopyFromMemory(mapSource.DataPointer, mapSource.RowPitch);
                    device.ImmediateContext.UnmapSubresource(captureTexture, 0);

                    rc_context.BeginDraw();
                    rc_context.DrawBitmap(outbmp, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
                    rc_context.EndDraw();

                    screenResource.Dispose();
                    duplicatedOutput.ReleaseFrame();
                }
            }
            catch (SharpDXException e)
            {
                if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                }
            }
        }
Example #27
0
        private void DrawTexture(SharpDX.Direct2D1.RenderTarget renderTarget, Texture2D texture)
        {
            using (var surf = texture.QueryInterface <SharpDX.DXGI.Surface1>())
            {
                var prop = new SharpDX.Direct2D1.BitmapProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied));
                SharpDX.Direct2D1.Bitmap screenBits = new SharpDX.Direct2D1.Bitmap(renderTarget, surf, prop);
                try
                {
                    var   srcDecr   = surf.Description;
                    float srcWidth  = srcDecr.Width;
                    float srcHeight = srcDecr.Height;

                    float destX      = 0;
                    float destY      = 0;
                    float destWidth  = DestSize.Width;
                    float destHeight = DestSize.Height;

                    float scaleX = destWidth / srcWidth;
                    float scaleY = destHeight / srcHeight;

                    if (AspectRatio)
                    {
                        if (scaleY < scaleX)
                        {
                            scaleX = scaleY;
                            destX  = ((destWidth - srcWidth * scaleX) / 2);
                        }
                        else
                        {
                            scaleY = scaleX;
                            destY  = ((destHeight - srcHeight * scaleY) / 2);
                        }
                    }

                    destWidth  = srcWidth * scaleX;
                    destHeight = srcHeight * scaleY;

                    var destRect = new SharpDX.Mathematics.Interop.RawRectangleF
                    {
                        Left   = destX,
                        Right  = destX + destWidth,
                        Top    = destY,
                        Bottom = destY + destHeight,
                    };

                    renderTarget.DrawBitmap(screenBits, destRect, 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
                }
                finally
                {
                    screenBits?.Dispose();
                }
            }
        }
Example #28
0
        public static SharpDX.Direct2D1.Bitmap ToBitmap(DUIRenderTarget renderTarget, Bitmap bitmap)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }
            System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
            var dataStream = new SharpDX.DataStream(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, true, false);
            var properties = new SharpDX.Direct2D1.BitmapProperties
            {
                PixelFormat = new SharpDX.Direct2D1.PixelFormat
                {
                    Format    = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied
                }
            };
            // ToDo apply scaling here!
            //var scaler = new SharpDX.WIC.BitmapScaler(renderTarget.Factory.NativePointer);
            //scaler.
            //Load the image from the gdi resource
            var result = new SharpDX.Direct2D1.Bitmap(renderTarget, new SharpDX.Size2(bitmap.Width, bitmap.Height), dataStream, bitmapData.Stride, properties);

            bitmap.UnlockBits(bitmapData);
            return(result);

            //System.Drawing.Bitmap desBitmap;//预定义要是使用的bitmap
            ////如果原始的图像像素格式不是32位带alpha通道,需要转换为32位带alpha通道的格式,否则无法和Direct2D的格式对应
            //if (bitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
            //{
            //    desBitmap = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            //    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(desBitmap))
            //    {
            //        g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, desBitmap.Width, desBitmap.Height), new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.GraphicsUnit.Pixel);
            //    }
            //}
            //else
            //{
            //    desBitmap = bitmap;
            //}
            ////直接内存copy会非常快
            ////如果使用循环逐点转换会非常慢
            //System.Drawing.Imaging.BitmapData bmpData = desBitmap.LockBits(new System.Drawing.Rectangle(0, 0, desBitmap.Width, desBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, desBitmap.PixelFormat);
            //int numBytes = bmpData.Stride * desBitmap.Height;
            //byte[] byteData = new byte[numBytes];
            //IntPtr ptr = bmpData.Scan0;
            //System.Runtime.InteropServices.Marshal.Copy(ptr, byteData, 0, numBytes);
            //desBitmap.UnlockBits(bmpData);
            //SharpDX.Direct2D1.PixelFormat pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            //SharpDX.Direct2D1.BitmapProperties bp = new SharpDX.Direct2D1.BitmapProperties(pixelFormat, desBitmap.HorizontalResolution, desBitmap.VerticalResolution);
            //SharpDX.Direct2D1.Bitmap tempBitmap = new SharpDX.Direct2D1.Bitmap(renderTarget, new SharpDX.Size2(desBitmap.Width, desBitmap.Height), bp);
            //tempBitmap.CopyFromMemory(byteData, bmpData.Stride);
            //return tempBitmap;
        }
Example #29
0
 public override void Dispose()
 {
     if (this.bitmap != null)
     {
         this.bitmap.Dispose();
         this.bitmap = null;
     }
     if (this.dxBitmap != null)
     {
         this.dxBitmap.Dispose();
         this.dxBitmap = null;
     }
     base.Dispose();
 }
Example #30
0
        public override void OnRenderTargetChanged()
        {
            base.OnRenderTargetChanged();
            if (RenderTarget == null || RenderTarget.IsDisposed)
            {
                return;
            }

            // Dispose all Render dependant resources on RenderTarget change.
            if (myBitmap != null)
            {
                myBitmap.Dispose();
            }
            if (fileStream != null)
            {
                fileStream.Dispose();
            }
            if (bitmapDecoder != null)
            {
                bitmapDecoder.Dispose();
            }
            if (converter != null)
            {
                converter.Dispose();
            }
            if (frame != null)
            {
                frame.Dispose();
            }

            // Neccessary for creating WIC objects.
            fileStream = new SharpDX.IO.NativeFileStream(System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SampleDrawBitmap.png"), SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
            // Used to read the image source file.
            bitmapDecoder = new SharpDX.WIC.BitmapDecoder(Core.Globals.WicImagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            // Get the first frame of the image.
            frame = bitmapDecoder.GetFrame(0);
            // Convert it to a compatible pixel format.
            converter = new SharpDX.WIC.FormatConverter(Core.Globals.WicImagingFactory);
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);
            // Create the new Bitmap1 directly from the FormatConverter.
            myBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarget, converter);

            if (!HWSet && myBitmap != null)
            {
                H     = myBitmap.Size.Height;
                W     = myBitmap.Size.Width;
                Ratio = W / H;
                HWSet = true;
            }
        }
Example #31
0
        protected override void OnRenderInitialized(RenderGraphics g)
        {
            theme = RenderTheme.CreateResourcesForGraphics(g);

            bmpTracks[Channel.Square1]  = g.CreateBitmapFromResource("Square");
            bmpTracks[Channel.Square2]  = g.CreateBitmapFromResource("Square");
            bmpTracks[Channel.Triangle] = g.CreateBitmapFromResource("Triangle");
            bmpTracks[Channel.Noise]    = g.CreateBitmapFromResource("Noise");
            bmpTracks[Channel.DPCM]     = g.CreateBitmapFromResource("DPCM");

            bmpGhostNote = g.CreateBitmapFromResource("GhostSmall");

            playPositionBrush    = g.CreateSolidBrush(Color.FromArgb(192, ThemeBase.LightGreyFillColor1));
            whiteKeyBrush        = g.CreateHorizontalGradientBrush(0, trackNameSizeX, ThemeBase.LightGreyFillColor1, ThemeBase.LightGreyFillColor2);
            patternHeaderBrush   = g.CreateVerticalGradientBrush(0, patternHeaderSizeY, ThemeBase.LightGreyFillColor1, ThemeBase.LightGreyFillColor2);
            selectedPatternBrush = g.CreateSolidBrush(Color.FromArgb(128, ThemeBase.LightGreyFillColor1));
        }
Example #32
0
        internal void Create()
        {
            if (m_bitmap == null)
                return;

            if (m_nativeBitmap != null)
            {
                m_nativeBitmap.Dispose();
                m_nativeBitmap = null;
            }
            
            var props = new SharpDX.Direct2D1.BitmapProperties();
            props.DpiX = m_bitmap.HorizontalResolution;
            props.DpiY = m_bitmap.VerticalResolution;            
            props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new DrawingSize(m_bitmap.Width, m_bitmap.Height), props);                        
            Update();
        }
Example #33
0
        internal D2dBitmap(D2dGraphics owner, int width, int height)
        {
            if (width < 1 || height < 1)
                throw new ArgumentOutOfRangeException("Width and height must be greater than zero");

            m_owner = owner;
            var props = new SharpDX.Direct2D1.BitmapProperties();
            props.DpiX = m_owner.DotsPerInch.Width;
            props.DpiY = m_owner.DotsPerInch.Height;
            props.PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            m_nativeBitmap = new SharpDX.Direct2D1.Bitmap(m_owner.D2dRenderTarget, new Size2(width, height), props);
            m_owner.RecreateResources += RecreateResources;
            m_rtNumber = owner.RenderTargetNumber;
        }