如何以矩形裁剪软件位图-UWP [英] How to crop a software bitmap in rectangular shape - UWP

查看:62
本文介绍了如何以矩形裁剪软件位图-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

这篇关于如何以矩形裁剪软件位图-UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆