2015-08-19 26 views
5

SharpDX kullanarak masaüstü ekran görüntüsü yakalamaya çalışıyorum. Uygulamam ekran görüntüsünü yakalayabiliyor ancak Windows Gezgini'nde etiketsiz. 2 çözüm denedim ama değişiklik yapmadan. Herhangi bir bilgiyi belgede bulmayı denedim, ama değişmeden. GönderenFileLabels görünür değil

:

PixelFormat.Format32bppArgb 

için: i çalışma çözüm bulduk araştırma ve googling kaç saat sonra

 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 biçimini kullanmayı denediniz mi? – Aybe

+0

Ben B8G8R8A8_UNorm kullanıyorum enum listesi PBGRA içinde bulunamadı, ancak PixelFormat Bitmap nesnesini PixelFormat.Format32bppRgb olarak değiştirdikten sonra o zaman çalışıyor. –

cevap

2

: Burada

enter image description here

mi kodudur

PixelFormat.Format32bppRgb 
İlgili konular