文件标签不可见。

5

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

enter image description here

这是我的代码:

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

你尝试过使用 PBGRA 格式吗? - aybe
我使用的是B8G8R8A8_UNorm,但在枚举列表PBGRA中找不到它。但是当我将Bitmap对象的PixelFormat更改为PixelFormat.Format32bppRgb后,它就可以正常工作了。 - Peter M.
1个回答

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

来自:

PixelFormat.Format32bppArgb

To:

PixelFormat.Format32bppRgb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接