我正在尝试在uwp应用程序中获取Rtsp视频的快照。要重现视频,我正在使用vlc.MediaElement库(欢迎对此提出更好的库的任何建议)。 目前,我正在使用这种方法获取视频快照:
private async void PhotoButton_Click(object sender, RoutedEventArgs e)
{
Media.Pause();
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Media, ((int)Media.Width / 2), ((int)Media.Height / 2));
ImgPreview.Source = renderTargetBitmap;
Media.Play();
}
ImgPreview元素中的结果不错,但是当我使用此功能将其保存到文件中时
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
StorageLibrary lib = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
StorageFolder folder = lib.SaveFolder;
StorageFile file = await folder.CreateFileAsync("TestPhoto_" + DateTime.Now.Ticks + ".jpg", CreationCollisionOption.GenerateUniqueName);
DisplayInformation display = DisplayInformation.GetForCurrentView();
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(ImgPreview);
IBuffer pixels = await rtb.GetPixelsAsync();
DataReader data = DataReader.FromBuffer(pixels);
byte[] bytes = new byte[pixels.Length];
data.ReadBytes(bytes);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
display.RawDpiX,
display.RawDpiY,
bytes);
await encoder.FlushAsync();
storage.doPut(file.Path, file.Name);
}
}
catch (Exception ex)
{
}
}
结果非常模糊,对于我的应用程序,我需要更高质量的图像。 我尝试使用dpi,但唯一的结果是完全移位了带有奇怪对角带的像素图。
有没有办法解决这个问题? 感谢您的帮助。
答案 0 :(得分:0)
结果非常模糊,对于我的应用程序,我需要更高质量的图像。
我检查了您的代码,发现获得了{strong> ImgPreview 的RenderTargetBitmap
,这是没有必要的,您可以直接使用MediaElement的RenderTargetBitmap来避免图像质量下降。
您将获得RenderTargetBitmap
的一半尺寸,其以下内容会导致图像质量下降。
await renderTargetBitmap.RenderAsync(Media, ((int)Media.Width / 2), ((int)Media.Height / 2));
请使用原尺寸。
await renderTargetBitmap.RenderAsync(Media, ((int)Media.Width), ((int)Media.Height));
......
private RenderTargetBitmap renderTargetBitmap;
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
StorageLibrary lib = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
StorageFolder folder = lib.SaveFolder;
StorageFile file = await folder.CreateFileAsync("TestPhoto_" + DateTime.Now.Ticks + ".jpg", CreationCollisionOption.GenerateUniqueName);
DisplayInformation display = DisplayInformation.GetForCurrentView();
IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
DataReader data = DataReader.FromBuffer(pixels);
byte[] bytes = new byte[pixels.Length];
data.ReadBytes(bytes);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
display.RawDpiX,
display.RawDpiY,
bytes);
await encoder.FlushAsync();
//storage.doPut(file.Path, file.Name);
}
}
catch (Exception ex)
{
}
}