将两张图像合并为一张新图像

发布于 2024-12-01 17:29:23 字数 155 浏览 3 评论 0原文

我有两个尺寸不同的 JPEG 文件:

Image1 (Width1,Height1)

Image2 (Width2,Height2)

我想创建 Image3 (Width3, Height3),其中 Image1 在左侧,Image2 在右侧。

I have two JPEG files with different dimensions:

Image1 (Width1,Height1)

Image2 (Width2,Height2)

I want to create Image3 (Width3, Height3) with Image1 on the left side and Image2 on the right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星光不落少年眉 2024-12-08 17:29:23

像这样的东西会给你一个新图像,其中两个原始图像并排。

Bitmap bitmap = new Bitmap(image1.Width + image2.Width, Math.Max(image1.Height, image2.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawImage(image1, 0, 0);
    g.DrawImage(image2, image1.Width, 0);
}

Something like this will give you a new image with the two original images side by side.

Bitmap bitmap = new Bitmap(image1.Width + image2.Width, Math.Max(image1.Height, image2.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawImage(image1, 0, 0);
    g.DrawImage(image2, image1.Width, 0);
}
只是偏爱你 2024-12-08 17:29:23

我有类似的问题。
使用此功能,您可以将多个位图合并为单个图像

    private Bitmap MergeImages(IEnumerable<Bitmap> images)
    {
        var enumerable = images as IList<Bitmap> ?? 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;
    }

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

    private Bitmap MergeImages(IEnumerable<Bitmap> images)
    {
        var enumerable = images as IList<Bitmap> ?? 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;
    }
幸福%小乖 2024-12-08 17:29:23

你可以试试

您需要使用的库

using System.Drawing;
使用系统.绘图.成像;
//控制器

public ActionResult Image()     
    {
        var bitmap = GetBitmap(); // The method that returns List<Bitmap>
        var width = 0;
        var height = 0;
        foreach (var image in bitmap)
        {
            width += image.Width;
            height = image.Height > height
                ? image.Height
                : height;
        }
        var bitmap2 = new Bitmap(width, height);
        var g = Graphics.FromImage(bitmap2);
        var localWidth = 0;
        foreach (var image in bitmap)
        {
            g.DrawImage(image, localWidth, 0);
            localWidth += image.Width;
        }

        var ms = new MemoryStream();

        bitmap2.Save(ms, ImageFormat.Png);
         var   result = ms.ToArray();
         //string base64String = Convert.ToBase64String(result); 
         return File(result, "image/jpeg"); //Return as file result
        //return base64String;
    }
//this method returns List<Bitmap>
public List<Bitmap> GetBitmap()
    {
        var lstbitmap = new List<Bitmap>();
        var bitmap = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\1525244892128.JPEG");
        var bitmap2 = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\1525244892204.JPEG");
        var bitmap3 = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\3.jpg");
        lstbitmap.Add(bitmap);
        lstbitmap.Add(bitmap2);
        lstbitmap.Add(bitmap3);
        return lstbitmap;
    }

祝你好运!

You can try it

library you need using

using System.Drawing;
using System.Drawing.Imaging;
//controller

public ActionResult Image()     
    {
        var bitmap = GetBitmap(); // The method that returns List<Bitmap>
        var width = 0;
        var height = 0;
        foreach (var image in bitmap)
        {
            width += image.Width;
            height = image.Height > height
                ? image.Height
                : height;
        }
        var bitmap2 = new Bitmap(width, height);
        var g = Graphics.FromImage(bitmap2);
        var localWidth = 0;
        foreach (var image in bitmap)
        {
            g.DrawImage(image, localWidth, 0);
            localWidth += image.Width;
        }

        var ms = new MemoryStream();

        bitmap2.Save(ms, ImageFormat.Png);
         var   result = ms.ToArray();
         //string base64String = Convert.ToBase64String(result); 
         return File(result, "image/jpeg"); //Return as file result
        //return base64String;
    }
//this method returns List<Bitmap>
public List<Bitmap> GetBitmap()
    {
        var lstbitmap = new List<Bitmap>();
        var bitmap = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\1525244892128.JPEG");
        var bitmap2 = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\1525244892204.JPEG");
        var bitmap3 = new Bitmap(@"E:\My project\ProjectImage\ProjectImage\BmImage\3.jpg");
        lstbitmap.Add(bitmap);
        lstbitmap.Add(bitmap2);
        lstbitmap.Add(bitmap3);
        return lstbitmap;
    }

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文