C#.net 2张图片合并输出

/// <summary>
    /// 调用此函数后使此两种图片合并,类似相册,有个
    /// 背景图,中间贴自己的目标图片
    /// </summary>
    /// <param name="sourceImg">粘贴的源图片</param>
    /// <param name="destImg">粘贴的目标图片</param>
    public static System.Drawing.Image CombinImage(string sourceImg, string destImg)
    {
        System.Drawing.Image imgBack = System.Drawing.Image.FromFile(sourceImg);     //相框图片 
        System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);        //照片图片



        //从指定的System.Drawing.Image创建新的System.Drawing.Graphics       
        Graphics g = Graphics.FromImage(imgBack);

        g.DrawImage(imgBack, 0, 0, 148, 124);      // g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
        //g.FillRectangle(System.Drawing.Brushes.Black, 16, 16, (int)112 + 2, ((int)73 + 2));//相片四周刷一层黑色边框



        //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
        g.DrawImage(img, 17, 17, 112, 73);
        GC.Collect();

        //输出文件流
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        imgBack.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ContentType = "image/png";
        System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
        imgBack.Dispose();
        return imgBack;
    }


参考应用网站【自贷通】

你可能感兴趣的:(C#.net 2张图片合并输出)