如何以矩形裁剪软件位图

编程入门 行业动态 更新时间:2024-10-26 11:22:54
本文介绍了如何以矩形裁剪软件位图-UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从正在运行的视频中捕获帧并将其转换为SoftwareBitmap,以用于进一步的目的.在此之前,我想将该框架裁剪为矩形.怎么可能?

I'am capturing a frame from running video and converting it to SoftwareBitmap for further purposes. Before that I want to crop that frame into a rectangular shape. How is it possible?

var thumbnail = await GetThumbnailAsync(file,seek_position); StringBuilder ocr=null; InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream(); await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream); randomAccessStream.Seek(0); SoftwareBitmap inputBitmap; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomAccessStream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(); //crop inputBitmap public async Task<IInputStream> GetThumbnailAsync(StorageFile file,int i) { //int duration_millisecond = i * 1000; var mediaClip = await MediaClip.CreateFromFileAsync(file); var mediaComposition = new MediaComposition(); mediaComposition.Clips.Add(mediaClip); return await mediaComposition.GetThumbnailAsync( TimeSpan.FromMilliseconds(i), 0, 0, VideoFramePrecision.NearestFrame); }

推荐答案

BitmapDecoder对象的 GetSoftwareBitmapAsync 方法具有多个重载方法.您可以使用 GetSoftwareBitmapAsync(BitmapPixelFormat,BitmapAlphaMode,BitmapTransform,ExifOrientationMode,ColorManagementMode)方法来裁剪软件位图.您只需要为其定义一个BitmapTransform对象.

The GetSoftwareBitmapAsync method of BitmapDecoder object has several overloaded methods. You could use GetSoftwareBitmapAsync(BitmapPixelFormat, BitmapAlphaMode, BitmapTransform, ExifOrientationMode, ColorManagementMode) method to crop the software bitmap. You just need to define a BitmapTransform object for it.

请参考以下代码示例:

SoftwareBitmap inputBitmap; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomAccessStream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat,BitmapAlphaMode.Ignore,new BitmapTransform() {Bounds=new BitmapBounds() {X=100,Y=200,Width=200,Height=100 } },ExifOrientationMode.IgnoreExifOrientation,ColorManagementMode.DoNotColorManage);

您只需要为其Bounds属性指定一个新的 BitmapBounds .

You just need to specify a new BitmapBounds to its Bounds property.

请注意,在此步骤中,您已经裁剪了软件位图,但是如果要使用它来初始化 SoftwareBitmapSource 并使其显示在 Image 中,控制.您将收到一个异常" SetBitmapAsync仅支持软件宽度/高度为正,bgra8像素格式且预乘或不包含alpha 的SoftwareBitmap.".您需要使用 SoftwareBitmap _softbitmap = SoftwareBitmap.Convert()来制作新的软件位图,如下所示:

Please note that, at this step, you have got a cropped software bitmap, but if you want to use it to initialize a SoftwareBitmapSource and make it show in Image control. You will get an exception "SetBitmapAsync only supports SoftwareBitmap with positive width/height, bgra8 pixel format and pre-multiplied or no alpha.". You need to use SoftwareBitmap _softbitmap = SoftwareBitmap.Convert() to make a new software bitmap like the following:

SoftwareBitmap _softbitmap = SoftwareBitmap.Convert(inputBitmap,BitmapPixelFormat.Bgra8,BitmapAlphaMode.Premultiplied); var source = new SoftwareBitmapSource(); await source.SetBitmapAsync(_softbitmap); image.Source = source; //image is a Image control

更多推荐

如何以矩形裁剪软件位图

本文发布于:2023-08-02 03:46:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   矩形   软件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!