Magick.NET 将2张图片合成一张,上下个一张,每张图片指定像素合并

发布时间 2023-12-08 17:38:34作者: 大树2

将2张图片合成一张,上下个一张,每张图片指定像素合并

1.net6应用nuget Magick.NET-Q16-anyCPU13.5
2.代码实现:
public PageResult MergoTwoImage(string product_ImageUrl= "http://138.8.183.172:8081/productImgs/01sku.jpg",
string auth_ImageUrl= "http://138.8.183.172:8081/authImgs/59888745/02categoryId.jpg")
{
Stopwatch st = new Stopwatch();
st.Start();
MagickImage image_product = null;
MagickImage image_auth = null;
MagickImageCollection imagesMergo = null;
IMagickImage result = null;
string fileName = "mergrImgs/";
try
{
//设置单个图片缩放大小
int imgWidth = 1024;
int imgHeith = 1024;
//读取图片
if (string.IsNullOrEmpty(product_ImageUrl)|| string.IsNullOrEmpty(auth_ImageUrl))
{
return new PageResult() { code = 400, msg = "image url is null", mergeImgPath = fileName };
//product_ImageUrl = "http://139.9.183.172:8081/productImgs/01sku.jpg";
//auth_ImageUrl = "http://139.9.183.172:8081/authImgs/59888745/02categoryId.jpg";
}
image_product = new MagickImage(@product_ImageUrl);
image_auth = new MagickImage(@auth_ImageUrl);

    //设置dpi
    image_product.Density = new Density(300.00, 300.00);
    image_auth.Density = new Density(300.00, 300.00);

    //修改图片尺寸(锁定比例,尺寸不一定是设置得尺寸)
    image_product.Resize(new MagickGeometry(imgWidth, imgHeith));
    image_auth.Resize(new MagickGeometry(imgWidth, imgHeith));

    //图片合并
    imagesMergo = new MagickImageCollection();
    imagesMergo.Add(image_product);
    imagesMergo.Add(image_auth);
    result = imagesMergo.AppendVertically();

    //图片保存
    fileName = "mergeImgs/nofilename.jpg";
    if (!string.IsNullOrEmpty(product_ImageUrl) && !string.IsNullOrEmpty(auth_ImageUrl))
    {
        fileName = "mergeImgs/";
        string[] arr_product= product_ImageUrl.Split("/");
        string[] arr_auth = auth_ImageUrl.Split("/");
        if(arr_product.Length>=5 && arr_auth.Length >= 6)
        {
            fileName += arr_auth[4] + "_" + arr_auth[5].Split(".")[0] + "_" + arr_product[4]; //mergrImgs/email前缀_categoryID_sku.jpg

            //test 生产20张图片用时
            //fileName += DateTime.Now.Millisecond.ToString() + "_" + arr_auth[4] + "_" + arr_auth[5].Split(".")[0] + "_" + arr_product[4]; //mergrImgs/email前缀_categoryID_sku.jpg
        }
        else
        {
            return new PageResult() { code = 400, msg = "error:input img url have error.", mergeImgPath = fileName };
        }
        
    }

    result.Write(@fileName);

    //string filename = DateTime.Now + DateTime.Now.Millisecond.ToString();
    //result.Write(@"images/" + filename + ".jpg"); filepath

    //返回byte[] 图片
    //var comparessedImg = result.ToByteArray(MagickFormat.Jpg);
    //返回base64图片
    //var comparessedImg2 = result.ToBase64(MagickFormat.Jpg);

    //清理图片对象
    image_product.Dispose();
    image_auth.Dispose();
    imagesMergo.Dispose();
    result.Dispose();

    st.Stop();
    return new PageResult() { code = 200, time = st.ElapsedMilliseconds, msg = "success,时间毫秒", mergeImgPath = fileName };
}
catch (Exception e)
{
    fileName = "mergrImgs/nofilename.jpg";
    return new PageResult() { code = 400, msg = "error,check url", mergeImgPath = fileName };
}
finally
{
    //清理图片对象
    if (image_product != null) image_product.Dispose();
    if (image_auth != null) image_auth.Dispose();
    if (imagesMergo != null) imagesMergo.Dispose();
    if (result != null) result.Dispose();
}

}已读