wpf 把两个Bitmap 合并为一个

时间:2022-01-30 19:37:29

水平合并

 /// <summary>
        /// 将两个Bitmap水平合并为一个
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        private Bitmap AddBitmap(Bitmap first, Bitmap second)
        {
            int width = first.Width + second.Width;
            int height = Math.Max(first.Height, second.Height);
            var newBitmap = new Bitmap(first.Width + second.Width, height);
            for (var i = 0; i <= first.Width - 1; i++)
            {
                for (var j = 0; j <= first.Height - 1; j++)
                {
                    var c = first.GetPixel(i, j);
                    newBitmap.SetPixel(i, j, c);
                }
            }

            for (var i = 0; i <= second.Width - 1; i++)
            {
                for (var j = 0; j <= second.Height - 1; j++)
                {
                    var c = second.GetPixel(i, j);
                    newBitmap.SetPixel(i + first.Width, j, c);
                }
            }

            first?.Dispose();
            second?.Dispose();
            return newBitmap;
        }

 

垂直合并;

/// <summary>
        /// 将两个Bitmap垂直合并为一个
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        private Bitmap AddBitmap(Bitmap first, Bitmap second)
        {
            int height = first.Height + second.Height;
            int width = Math.Max(first.Width, second.Width);
            var newBitmap = new Bitmap(width, height);
            for (var i = 0; i <= first.Width - 1; i++)
            {
                for (var j = 0; j <= first.Height - 1; j++)
                {
                    var c = first.GetPixel(i, j);
                    newBitmap.SetPixel(i, j, c);
                }
            }

            for (var i = 0; i <= second.Width - 1; i++)
            {
                for (var j = 0; j <= second.Height - 1; j++)
                {
                    var c = second.GetPixel(i, j);
                    newBitmap.SetPixel(i, j + first.Height, c);
                }
            }

            first?.Dispose();
            second?.Dispose();
            return newBitmap;
        }