c# - 使用 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>
解决方案
利用
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();
}
推荐阅读
- ios - Xcode 12 (beta) 编译器错误 - 两个具有相同名称但在不同模块中的类
- swift - 如何根据图库中的图像快速调整imageView的大小
- python - Jupyter notebook 在(imshow)图上移动矩形
- amazon-web-services - 从 CloudWatch 获取最新指标
- javascript - JS - 从链接获取 MIME 类型
- python - Python/subprocess:从批处理文件读取输出失败
- java - 如何确定 Quartz Job 是否暂停
- vue.js - 基于道具项目格式化类星体表中的行
- android-studio - 找不到模块“比较功能”Cordova Android
- python - 在 Python 中加权调查变量?