public Bitmap ConvertToBitmap(string filePath)
        {
            using (var texture = Texture.FromFile(device, filePath)) {
            var surfaceDescription = texture.GetLevelDescription(0);
            var textureWidth = surfaceDescription.Width;
            var textureHeight = surfaceDescription.Height;

            using (var renderTarget = Surface.CreateRenderTarget(device, textureWidth, textureHeight, Format.X8R8G8B8, MultisampleType.None, 0, true)) {
               var oldBackBuffer = device.GetRenderTarget(0);

               device.SetRenderTarget(0, renderTarget);
               using (var sprite = new Sprite(device)) {
                  device.BeginScene();
                  sprite.Begin(SpriteFlags.AlphaBlend);
                  sprite.Draw(texture, new ColorBGRA(Vector4.One));
                  sprite.End();
                  device.EndScene();
               }
               device.SetRenderTarget(0, oldBackBuffer);

               var renderTargetData = renderTarget.LockRectangle(LockFlags.ReadOnly);
               var resultBitmap = new Bitmap(textureWidth, textureHeight);
               var resultData = resultBitmap.LockBits(new Rectangle(0, 0, textureWidth, textureHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
               for (var y = 0; y < textureHeight; y++) {
                  Utilities.CopyMemory(resultData.Scan0 + y * resultData.Stride, renderTargetData.DataPointer + y * renderTargetData.Pitch, textureWidth * 4);
               }
               resultBitmap.UnlockBits(resultData);
               renderTarget.UnlockRectangle();
               return resultBitmap;
            }
             }
        }
Esempio n. 2
0
    public void DrawOverlay(Texture targetTexture)
    {
      Subtitle currentSubtitle;
      lock (_syncObj)
      {
        currentSubtitle = _subtitles.ToList().FirstOrDefault(s => s.ShouldDraw);
        if (currentSubtitle == null)
          return;

        if (targetTexture == null || targetTexture.IsDisposed || currentSubtitle.SubTexture == null || currentSubtitle.SubTexture.IsDisposed)
        {
          if (_drawCount > 0)
            ServiceRegistration.Get<ILogger>().Debug("Draw count for last sub: {0}", _drawCount);
          _drawCount = 0;
          return;
        }
        _drawCount++;
      }

      try
      {
        // TemporaryRenderTarget changes RenderTarget to texture and restores settings when done (Dispose)
        using (new TemporaryRenderTarget(targetTexture))
        using (TemporaryRenderState temporaryRenderState = new TemporaryRenderState())
        using (Sprite sprite = new Sprite(_device))
        {
          sprite.Begin(SpriteFlags.AlphaBlend);
          // No alpha test here, allow all values
          temporaryRenderState.SetTemporaryRenderState(RenderState.AlphaTestEnable, 0);

          // Use the SourceAlpha channel and InverseSourceAlpha for destination
          temporaryRenderState.SetTemporaryRenderState(RenderState.BlendOperation, (int)BlendOperation.Add);
          temporaryRenderState.SetTemporaryRenderState(RenderState.SourceBlend, (int)Blend.SourceAlpha);
          temporaryRenderState.SetTemporaryRenderState(RenderState.DestinationBlend, (int)Blend.InverseSourceAlpha);

          // Check the target texture dimensions and adjust scaling and translation
          SurfaceDescription desc = targetTexture.GetLevelDescription(0);
          Matrix transform = Matrix.Identity;

          // Position subtitle and scale it to match video frame size, if required
          transform *= Matrix.Translation(currentSubtitle.HorizontalPosition, currentSubtitle.FirstScanLine, 0);

          if (currentSubtitle.ScreenWidth != desc.Width || currentSubtitle.ScreenHeight != desc.Height)
          {
            var factorW = (float)desc.Width / currentSubtitle.ScreenWidth;
            var factorH = (float)desc.Height / currentSubtitle.ScreenHeight;
            transform *= Matrix.Scaling(factorW, factorH, 1);
          }

          sprite.Transform = transform;
          sprite.Draw(currentSubtitle.SubTexture, SharpDX.Color.White);
          sprite.End();
        }

        if (_onTextureInvalidated != null)
          _onTextureInvalidated();
      }
      catch (Exception ex)
      {
        ServiceRegistration.Get<ILogger>().Debug("Error in DrawOverlay", ex);
      }
    }
Esempio n. 3
0
    public void DrawOverlay(Surface targetSurface)
    {
      Texture subTexture;
      Subtitle currentSubtitle;
      lock (_syncObj)
      {
        currentSubtitle = _currentSubtitle;
        subTexture = _subTexture;
        // Available sub is not due
        if (currentSubtitle == null || !currentSubtitle.ShouldDraw)
          return;

        if (targetSurface == null || targetSurface.IsDisposed || subTexture == null)
        {
          if (_drawCount > 0)
            ServiceRegistration.Get<ILogger>().Debug("Draw count for last sub: {0}", _drawCount);
          _drawCount = 0;
          return;
        }
        _drawCount++;
      }

      try
      {
        if (!subTexture.IsDisposed)
        {
          // TemporaryRenderTarget changes RenderTarget to texture and restores settings when done (Dispose)
          using (new TemporaryRenderTarget(targetSurface))
          using (TemporaryRenderState temporaryRenderState = new TemporaryRenderState())
          using (Sprite sprite = new Sprite(_device))
          {
            sprite.Begin(SpriteFlags.AlphaBlend);
            // No alpha test here, allow all values
            temporaryRenderState.SetTemporaryRenderState(RenderState.AlphaTestEnable, 0);

            // Use the SourceAlpha channel and InverseSourceAlpha for destination
            temporaryRenderState.SetTemporaryRenderState(RenderState.BlendOperation, (int) BlendOperation.Add);
            temporaryRenderState.SetTemporaryRenderState(RenderState.SourceBlend, (int) Blend.SourceAlpha);
            temporaryRenderState.SetTemporaryRenderState(RenderState.DestinationBlend, (int) Blend.InverseSourceAlpha);

            // Check the target texture dimensions and adjust scaling and translation
            SurfaceDescription desc = targetSurface.Description;
            Matrix transform = Matrix.Identity;
            transform *= Matrix.Translation(currentSubtitle.HorizontalPosition, currentSubtitle.FirstScanLine, 0);

            // Subtitle could be smaller for 16:9 anamorphic video (subtitle width: 720, video texture: 1024)
            // then we need to scale the subtitle width also.
            if (currentSubtitle.ScreenWidth != desc.Width)
              transform *= Matrix.Scaling((float) desc.Width / currentSubtitle.ScreenWidth, 1, 1);

            sprite.Transform = transform;
            sprite.Draw(subTexture, SharpDX.Color.White);
            sprite.End();
          }
        }
      }
      catch (Exception ex)
      {
        ServiceRegistration.Get<ILogger>().Debug("Error in DrawOverlay", ex);
      }

      if (_onTextureInvalidated != null)
        _onTextureInvalidated();
    }