PR

UWP SoftwareBitmap 切抜き,トリミング

CSharp用のアイキャッチ UWP
スポンサーリンク

備忘録

UWPの開発はなかなか参考サイトなど見つけにくいので備忘録

MediaCaptureを使用してバーコードの読み取りアプリを開発中なんですが、どうにも読み取り完了までに時間がかかってしまう。
そこで画像を切り抜いてからDecodeしたほうが高速になるんじゃないかと思って調べました。

コードがこちら

コードを少しいじってますがほぼ原文のまま
(BitmapAlphaModeをPremultipliedで固定したくらい SoftwareBitmapSourceで使いたかったので)

    public async static Task<SoftwareBitmap> GetCroppedBitmapAsync(SoftwareBitmap softwareBitmap, uint startPointX, uint startPointY, uint width, uint height)
    {
        using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);

            encoder.SetSoftwareBitmap(softwareBitmap);

            encoder.BitmapTransform.Bounds = new BitmapBounds()
            {
                X = startPointX,
                Y = startPointY,
                Height = height,
                Width = width
            };


            await encoder.FlushAsync();

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

            return await decoder.GetSoftwareBitmapAsync(softwareBitmap.BitmapPixelFormat, BitmapAlphaMode.Premultiplied);
        }
    }

メモ

UWP開発やめれば楽になりそうな気もしますが、Windowsが好きなのでガンバリます。

参照サイト

https://stackoverrun.com/ja/q/11161682

コメント

タイトルとURLをコピーしました