2015-08-19 41 views
5

我嘗試使用SharpDX捕獲桌面屏幕截圖。我的應用程序能夠捕獲截圖,但在Windows資源管理器中沒有標籤。 我嘗試了2個解決方案,但沒有改變。我試着在文檔中找到任何信息,但沒有改變。FileLabels不可見

enter image description here

這裏是MI代碼:

 public void SCR() 
    { 
     uint numAdapter = 0; // # of graphics card adapter 
     uint numOutput = 0; // # of output device (i.e. monitor) 

     // create device and factory 

     var device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware); 
     var factory = new Factory1(); 

     // creating CPU-accessible texture resource 
     var texdes = new SharpDX.Direct3D11.Texture2DDescription 
     { 
      CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read, 
      BindFlags = SharpDX.Direct3D11.BindFlags.None, 
      Format = Format.B8G8R8A8_UNorm, 
      Height = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Height, 
      Width = factory.Adapters1[numAdapter].Outputs[numOutput].Description.DesktopBounds.Width, 
      OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None, 
      MipLevels = 1, 
      ArraySize = 1 
     }; 
     texdes.SampleDescription.Count = 1; 
     texdes.SampleDescription.Quality = 0; 
     texdes.Usage = SharpDX.Direct3D11.ResourceUsage.Staging; 
     var screenTexture = new SharpDX.Direct3D11.Texture2D(device, texdes); 

     // duplicate output stuff 
     var output = new Output1(factory.Adapters1[numAdapter].Outputs[numOutput].NativePointer); 
     var duplicatedOutput = output.DuplicateOutput(device); 
     SharpDX.DXGI.Resource screenResource = null; 
     SharpDX.DataStream dataStream; 
     Surface screenSurface; 
     var i = 0; 

     var miliseconds = 2500000; 
     while (true) 
     { 
      i++; 
      // try to get duplicated frame within given time 
      try 
      { 
       SharpDX.DXGI.OutputDuplicateFrameInformation duplicateFrameInformation; 
       duplicatedOutput.AcquireNextFrame(miliseconds, out duplicateFrameInformation, out screenResource); 
      } 
      catch (SharpDX.SharpDXException e) 
      { 
       if (e.ResultCode.Code == SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code) 
       { 
        // this has not been a successful capture 
        // thanks @Randy 

        // keep retrying 
        continue; 
       } 
       else 
       { 
        throw e; 
       } 
      } 

      device.ImmediateContext.CopyResource(screenResource.QueryInterface<SharpDX.Direct3D11.Resource>(), screenTexture); 
      screenSurface = screenTexture.QueryInterface<Surface>(); 


      // screenSurface.Map(SharpDX.DXGI.MapFlags.Read, out dataStream); 

      int width = output.Description.DesktopBounds.Width; 
      int height = output.Description.DesktopBounds.Height; 
      var boundsRect = new System.Drawing.Rectangle(0, 0, width, height); 
      var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None); 
      using (var bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format32bppArgb)) 
      { 
       // Copy pixels from screen capture Texture to GDI bitmap 
       var bitmapData = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat); 
       var sourcePtr = mapSource.DataPointer; 
       var destinationPtr = bitmapData.Scan0; 
       for (int y = 0; y < height; y++) 
       { 
        // Copy a single line 
        Utilities.CopyMemory(destinationPtr, sourcePtr, width * 4); 

        // Advance pointers 
        sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch); 
        destinationPtr = IntPtr.Add(destinationPtr, bitmapData.Stride); 
       } 

       // Release source and dest locks 
       bitmap.UnlockBits(bitmapData); 

       device.ImmediateContext.UnmapSubresource(screenTexture, 0); 

       bitmap.Save(string.Format(@"d:\scr\{0}.png", i)); 
      } 


      // var image = FromByte(ToByte(dataStream)); 

      //var image = getImageFromDXStream(1920, 1200, dataStream); 
      //image.Save(string.Format(@"d:\scr\{0}.png", i)); 

      // dataStream.Close(); 
      //screenSurface.Unmap(); 
      screenSurface.Dispose(); 
      screenResource.Dispose(); 
      duplicatedOutput.ReleaseFrame(); 
     } 
    } 
+0

您是否嘗試過使用PBGRA格式? – Aybe

+0

我使用B8G8R8A8_UNorm我沒有在枚舉列表PBGRA中找到,但是在將PixelFormat的Bitmap對象更改爲PixelFormat.Format32bppRgb後,它正在工作。 –

回答

2

的研究和谷歌搜索幾個小時後,我發現工作液:

來源:

PixelFormat.Format32bppArgb 

要:

PixelFormat.Format32bppRgb