Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UWP中RenderTargetBitmap.RenderAsync中图像的宽度和高度_Uwp - Fatal编程技术网

UWP中RenderTargetBitmap.RenderAsync中图像的宽度和高度

UWP中RenderTargetBitmap.RenderAsync中图像的宽度和高度,uwp,Uwp,我发现了这个:但这是WPF而不是UWP RenderTargetBitmap类有一个RenderAsync方法(不接受宽度和高度)和一个同名的方法(接受宽度和高度)。宽度和高度没有达到我的预期。比如说, 不使用宽度和高度,我的网格以2342 x 1262的速度渲染,输出文件的比例与我在屏幕上看到的比例相同 使用20000 x 10777的宽度和高度,我的栅格以4096 x 2208进行渲染 使用3000 x 1616的宽度和高度,我的栅格仍以4096 x 2207进行渲染 使用3000 x 4

我发现了这个:但这是WPF而不是UWP

RenderTargetBitmap类有一个RenderAsync方法(不接受宽度和高度)和一个同名的方法(接受宽度和高度)。宽度和高度没有达到我的预期。比如说,

  • 不使用宽度和高度,我的网格以2342 x 1262的速度渲染,输出文件的比例与我在屏幕上看到的比例相同

  • 使用20000 x 10777的宽度和高度,我的栅格以4096 x 2208进行渲染

  • 使用3000 x 1616的宽度和高度,我的栅格仍以4096 x 2207进行渲染

  • 使用3000 x 4000的宽度和高度,我的栅格渲染为3072 x 4096

  • 使用1000 x 538的宽度和高度,我的栅格渲染为1500 x 807

所以这些数字起了作用,但我不知道使用什么规则来拾取RenderTargetBitmap的最终像素宽度和像素高度。它们似乎是最小值

这是我的渲染方法,以防我做错了其他事情。请注意,我尝试采用建议的输入大小,并使它们与渲染时获得的大小成比例,而不提供大小。另外值得注意的是,输入元素是一个网格,包含在屏幕上的ViewBox元素中

    private async Task CreateSaveBitmapAsync( UIElement Element, int SuggestedWidth, int SuggestedHeight )
    {
        if (Element != null)
        {
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync( Element );

            double WidthRatio = renderTargetBitmap.PixelWidth / (double)renderTargetBitmap.PixelHeight;

            if( WidthRatio >= 1 )
                SuggestedHeight = (int)( SuggestedWidth / WidthRatio );
            else
                SuggestedWidth = (int)( SuggestedHeight * WidthRatio );

            await renderTargetBitmap.RenderAsync( Element, SuggestedWidth, SuggestedHeight );

            var picker = new FileSavePicker();
            picker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
            StorageFile file = await picker.PickSaveFileAsync();
            if (file != null)
            {
                var pixels = await renderTargetBitmap.GetPixelsAsync();

                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await
                        BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
                    byte[] bytes = pixels.ToArray();
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight,
                                         96, 96, bytes);

                    await encoder.FlushAsync();
                }
            } 
        }
    }

仔细查看文档后,我发现有一个内置的最大大小,不能超过。所以我只是不明白为什么图像会比我输入的数字更大。这些是最小值吗?

我自己最近遇到了这个问题,发现RenderTargetBitmap受监视器当前DPI设置的影响

根据您提供的值,我可以假设您的DPI为150%。我不知道max 4096x4096的尺寸,但看起来这是需要考虑的另一个参数


在我的例子中,我需要一个特定大小的图像(对于活动平铺),所以我检查渲染的位图大小,如果它不符合我的预期大小,我将在代码中调整它的大小。如果您需要代码,请告诉我。

我最近也遇到了这个问题,发现RenderTargetBitmap受监视器当前DPI设置的影响

根据您提供的值,我可以假设您的DPI为150%。我不知道max 4096x4096的尺寸,但看起来这是需要考虑的另一个参数


在我的例子中,我需要一个特定大小的图像(对于活动平铺),所以我检查渲染的位图大小,如果它不符合我的预期大小,我将在代码中调整它的大小。如果您需要代码,请告诉我。

根据下页,这是DirectX的一个限制。

根据下页,这是DirectX的一个限制。

很好地确定了我的DPI设置。你说的完全正确。我没有注意到150%的变化,因为我经常达到4096的最大值。4096最大值来自Direct3D渲染纹理最大值,根据我在某处读到的内容。很好地了解了我的DPI设置。你说的完全正确。我没有注意到150%的变化,因为我经常达到4096的最大值。4096最大值来自Direct3D渲染纹理最大值,根据我在某处读到的内容。