Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileLabels are not visible

Tags:

c#

sharpdx

I try to capture desktop screenshot using SharpDX. My application is able to capture screenshot but without labels in Windows Explorer. I tryed 2 solutions but without change. I tried find in documentation any information, but without change.

enter image description here

Here is mi code:

        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();
        }
    }
like image 547
Peter M. Avatar asked Oct 31 '22 21:10

Peter M.


1 Answers

After few hours of research and googling i found working solution:

From:

PixelFormat.Format32bppArgb

To:

PixelFormat.Format32bppRgb
like image 189
Peter M. Avatar answered Nov 15 '22 06:11

Peter M.