UWP中RenderTargetBitmap.RenderAsync中图像的宽度和高度
我发现了这个:但这是WPF而不是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
- 不使用宽度和高度,我的网格以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
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渲染纹理最大值,根据我在某处读到的内容。