FileLabel不可见

时间:2015-08-19 15:33:53

标签: c# sharpdx

我尝试使用SharpDX捕获桌面截图。我的应用程序能够捕获屏幕截图但在Windows资源管理器中没有标签。 我尝试了2种解决方案,但没有改变。我尝试在文档中查找任何信息,但不做任何更改。

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();
        }
    }

1 个答案:

答案 0 :(得分:2)

经过几个小时的研究和谷歌搜索,我找到了可行的解决方案:

自:

PixelFormat.Format32bppArgb

要:

PixelFormat.Format32bppRgb