Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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
C# 使用RenderTargetBitmap或Capture UI时如何获得真实大小的图像_C#_Uwp_Uwp Xaml - Fatal编程技术网

C# 使用RenderTargetBitmap或Capture UI时如何获得真实大小的图像

C# 使用RenderTargetBitmap或Capture UI时如何获得真实大小的图像,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,我希望在使用RenderTargetBitmap或捕获UI时获得真实大小的图像,因为在使用RenderTargetBitmap或捕获UI时。图像模糊且不清晰,但当我将图像扩展到原始大小时,图像不会模糊且清晰 RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(ImageAndSticker,250

我希望在使用RenderTargetBitmap或捕获UI时获得真实大小的图像,因为在使用RenderTargetBitmap或捕获UI时。图像模糊且不清晰,但当我将图像扩展到原始大小时,图像不会模糊且清晰

            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(ImageAndSticker,2500,3750);
            StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("snapshot" + DateTime.Now.ToString("MM-dd-yyyy h.mm.ss.fff tt") + ".jpg", CreationCollisionOption.GenerateUniqueName);
            storageFile = file;
            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            var pixels = pixelBuffer.ToArray();
            var displayInformation = DisplayInformation.GetForCurrentView();

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, pixels);
                await encoder.FlushAsync();
            }

使用

frameimage.Source.Width 
获取源图像的宽度

对高度执行的相同操作将获得源图像的高度值,如下所示

frameImage.Source.Height
我想您希望将源图像保存为文件

如果是,请使用此代码

BitmapSource source = frameImage.Source as BitmapSource;

using ( var FileStream = new FileStream( FileLoc, FileMode.Create, FileAccess.ReadWrite) ) {
    BitmapEncoder Encoder = new JpegBitmapEncoder();
    Encoder.Frames.Add(BitmapFrame.Create(source));
    Encoder.Save(FileStream);
    FileStream.Dispose();
}

我尝试你的代码是错误的。我不知道如何解决您的代码。如果您已将
RenderTargetBitmap
存储到该文件中,则可以使用获取包含实际宽度和高度信息的图像属性。能否显示示例代码?请检查上一行,它包含示例代码。
BitmapSource source = frameImage.Source as BitmapSource;

using ( var FileStream = new FileStream( FileLoc, FileMode.Create, FileAccess.ReadWrite) ) {
    BitmapEncoder Encoder = new JpegBitmapEncoder();
    Encoder.Frames.Add(BitmapFrame.Create(source));
    Encoder.Save(FileStream);
    FileStream.Dispose();
}