在WPF中创建一个复合BitmapImage [英] Create a Composite BitmapImage in WPF

查看:545
本文介绍了在WPF中创建一个复合BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将三个BitmapImage缝合在一起以创建一个合成图像.将要缝合在一起的三个图像按以下方式对齐:

I have three BitmapImages that I would like to stitch together to create a composite image. The three images to be stitched together are aligned in the following way:

图像的类型为System.Windows.Media.Imaging.BitmapImage.我看过以下解决方案,但它使用的是System .Drawing.Graphics执行拼接.我发现每次我想将它们映射在一起时,将我的BitmapImage转换为System.Drawing.Bitmap都是不直观的.

The images are of a type System.Windows.Media.Imaging.BitmapImage. I have looked at the following solution, but it uses System.Drawing.Graphics to perform stitching. I find it unintuitive to convert my BitmapImage to System.Drawing.Bitmap everytime I want to stich them together.

是否有一种简单的方法将三个System.Windows.Media.Imaging.BitmapImage类型的图像缝合在一起?

Is there a simple way to stitch three images of type System.Windows.Media.Imaging.BitmapImage together?

推荐答案

除了其他答案中描述的选项之外,下面的代码还将三个BitmapSource缝合到一个WriteableBitmap中:

In addition to the options described in the other answer, the code below stitches three BitmapSource together into a single WriteableBitmap:

public BitmapSource StitchBitmaps(BitmapSource b1, BitmapSource b2, BitmapSource b3)
{
    if (b1.Format != b2.Format || b1.Format != b3.Format)
    {
        throw new ArgumentException("All input bitmaps must have the same pixel format");
    }

    var width = Math.Max(b1.PixelWidth, b2.PixelWidth + b3.PixelWidth);
    var height = b1.PixelHeight + Math.Max(b2.PixelHeight, b3.PixelHeight);
    var wb = new WriteableBitmap(width, height, 96, 96, b1.Format, null);
    var stride1 = (b1.PixelWidth * b1.Format.BitsPerPixel + 7) / 8;
    var stride2 = (b2.PixelWidth * b2.Format.BitsPerPixel + 7) / 8;
    var stride3 = (b3.PixelWidth * b3.Format.BitsPerPixel + 7) / 8;
    var size = b1.PixelHeight * stride1;
    size = Math.Max(size, b2.PixelHeight * stride2);
    size = Math.Max(size, b3.PixelHeight * stride3);

    var buffer = new byte[size];
    b1.CopyPixels(buffer, stride1, 0);
    wb.WritePixels(
        new Int32Rect(0, 0, b1.PixelWidth, b1.PixelHeight),
        buffer, stride1, 0);

    b2.CopyPixels(buffer, stride2, 0);
    wb.WritePixels(
        new Int32Rect(0, b1.PixelHeight, b2.PixelWidth, b2.PixelHeight),
        buffer, stride2, 0);

    b3.CopyPixels(buffer, stride3, 0);
    wb.WritePixels(
        new Int32Rect(b2.PixelWidth, b1.PixelHeight, b3.PixelWidth, b3.PixelHeight),
        buffer, stride3, 0);

    return wb;
}

这篇关于在WPF中创建一个复合BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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