Combine two Images into one new Image

后端 未结 3 1108
暗喜
暗喜 2020-11-29 07:57

I have two JPEG files with different dimensions:

Image1 (Width1,Height1)

Image2 (Width2,Height2)

I want to create Image3 (Width3, Height3) with Image

3条回答
  •  渐次进展
    2020-11-29 08:23

    I had a similar problem. With this function you can merge multiple Bitmap's into a single image

        private Bitmap MergeImages(IEnumerable images)
        {
            var enumerable = images as IList ?? images.ToList();
    
            var width = 0;
            var height = 0;
    
            foreach (var image in enumerable)
            {
                width += image.Width;
                height = image.Height > height
                    ? image.Height
                    : height;
            }
    
            var bitmap = new Bitmap(width, height);
            using (var g = Graphics.FromImage(bitmap))
            {
                var localWidth = 0;
                foreach (var image in enumerable)
                {
                    g.DrawImage(image, localWidth, 0);
                    localWidth += image.Width;
                }
            }
            return bitmap;
        }
    

提交回复
热议问题