将位图数组合并到单个位图中 [英] Merge an array of Bitmaps into a single Bitmap

查看:151
本文介绍了将位图数组合并到单个位图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Bitmap 的数组合并到一个位图中。给出 Bitmap [,] 数组 b ,如下所示(假设这些图像看起来像字符):

I'm trying to merge an array of Bitmaps into a single Bitmap. Given a Bitmap[,] array b like the following (assume these are images that look like characters):

b[0,0] = 1
b[1,0] = 2
b[0,1] = 3
b[1,1] = 4

我想生成

result = 12
         34

例如,给定以下四个位图 s:

For example, given the following four Bitmaps:

b [0,0] = ;

b [1,0] = < img src =https://i.imgur.com/xy7YQ.pngalt =图像1,1title =示例图像2> ;

b [0,1] = ;

b [1,1] = ;

我想生成
result = ;

这是我到目前为止的代码:

Here's my code so far:

    public static Bitmap Moisac(ref Bitmap[,] b)
    {
        BitmapData[,] bmData = new BitmapData[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1];
        IntPtr[,] scan0 = new IntPtr[b.GetUpperBound(0) + 1, b.GetUpperBound(1) + 1];
        unsafe
        {
            byte*[,] p = new byte*[b.GetUpperBound(0) + 1,b.GetUpperBound(1) + 1];
            for (int i = 0; i <= b.GetUpperBound(0); i++)
                for (int j = 0; j <= b.GetUpperBound(1); j++)
                    if (b[i, j].Width != b[0, 0].Width | b[i, j].Height != b[0, 0].Height)
                        throw new ArgumentException(
                            "Width and Height properties of all elements of b must be equal.",
                            "b");

            int oneW = b[0, 0].Width;
            int oneH = b[0, 0].Height;
            int overallWidth = oneW * (b.GetUpperBound(0) + 1);
            int overallHeight = oneH * (b.GetUpperBound(1) + 1);
            Bitmap result = new Bitmap(b[0, 0], overallWidth, overallHeight);

            for (int i = 0; i <= b.GetUpperBound(0); i++)
                for (int j = 0; j <= b.GetUpperBound(1); j++)
                {
                    bmData[i, j] = b[i, j].LockBits(new Rectangle(0, 0, oneW, oneH),
                                                    ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                    scan0[i, j] = bmData[i, j].Scan0;
                    p[i, j] = (byte*)(void*)scan0[i, j];
                }

            BitmapData rbmData = result.LockBits(new Rectangle(0, 0, overallWidth, overallHeight),
                                                 ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData[0, 0].Stride;
            int nOffset = stride - 3*b[0, 0].Width;

            int rStride = rbmData.Stride;
            IntPtr rScan0 = rbmData.Scan0;
            byte* rp = (byte*) (void*) rScan0;

            for (int imgY = 0; imgY < b.GetUpperBound(1); ++imgY)
            {
                for (int imgX = 0; imgX <= b.GetUpperBound(0); ++imgX)
                {
                    byte* currp = p[imgX, imgY];
                    for (int y = 0; y < oneH; ++y)
                    {
                        for (int x = 0; x < 3*oneW; ++x)
                        {
                            rp[rStride*(imgY*oneH + y) + 3*imgX*oneW + x] = currp[0];
                            ++currp;
                        }
                        currp += nOffset;
                    }
                }
            }

            for (int i = 0; i <= b.GetUpperBound(0); i++)
                for (int j = 0; j <= b.GetUpperBound(1); j++)
                    b[i, j].UnlockBits(bmData[i,j]);


            result.UnlockBits(rbmData);
            return result;
        }
    }

查看相册中的图片这里。所有这些都不会在这里显示。

See the images in the album here. All of them won't display here.

推荐答案

我犯了最愚蠢的错误。但是,如果它可以帮助任何人,请更改

I made the most stupid mistake ever. However, if it may help anyone, change

for (int imgY = 0; imgY < b.GetUpperBound(1); ++imgY)

for (int imgY = 0; imgY <= b.GetUpperBound(1); ++imgY)

< 应为< = )。

这篇关于将位图数组合并到单个位图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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