首页 > 解决方案 > 使用 RenderTargetBitmap 或 Capture UI 时如何获得真实尺寸的图像

问题描述

我想在使用 RenderTargetBitmap 或 Capture UI 时获得真实尺寸的图像,因为当我使用 RenderTargetBitmap 或 Capture 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();
            }
           <Viewbox Margin="254.8,8,659,474" Stretch="Uniform" StretchDirection="Both"  x:Name="ViewImage" Grid.Column="1"  Grid.Row="0">
           <Image x:Name="frameimage" Margin="176.8,2,459,135" Grid.Column="1" Height="3750" Width="2500" Canvas.Left="-458" Canvas.Top="-641"/> 
           </Viewbox>

在此处输入图像描述

标签: c#uwpuwp-xaml

解决方案


利用

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

推荐阅读