下载该示例代码

文章出处:http://www.cnblogs.com/kaixin110/archive/2007/08/04/841827.html

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Haix.Utils
{
    class GenerateImage
    {        
        public struct favoriteImage
        
            private string _imagePath;            
            private int _x;
            private int _y;
            
            public int x
            {
                get 
                {
                    return _x; 
                }
                set 
                {
                    _x  = value;
                }
            }

            public int y
            {
                get
                {
                    return _y;
                }
                set
                {
                    _y = value;
                }
            }

            public string imagePath
            {
                get
                {
                    return _imagePath;
                }
                set
                {
                    _imagePath = value;
                }
            }
        }        

        [STAThread]
        static void Main(string[] args)
        {
            string CurrentDirectory = System.Environment.CurrentDirectory;
            string body_path=CurrentDirectory + "//white.png"; 

            favoriteImage[] FaImage = new favoriteImage[2];
            
            FaImage[0].x = -3;
            FaImage[0].y = 70;
            FaImage[0].imagePath = CurrentDirectory + "//1.png";

            FaImage[1].x = 20;//65;
            FaImage[1].y = -12;
            FaImage[1].imagePath = CurrentDirectory + "//2.png";

            generateWinterMark(CurrentDirectory,body_path, FaImage);
        }

        /**//// <summary>
        /// 生成水印
        /// </summary>
        /// <param name="Main">主图片路径,eg:body</param>
        /// <param name="Child">要叠加的图片路径</param>
        /// <param name="x">要叠加的图片位置的X坐标</param>
        /// <param name="y">要叠加的图片位置的Y坐标</param>
        /// <param name="isSave"></param>
        /// <returns>生成图片的路径</returns>        
        private static string generateWinterMark(string savePath,string body_path,favoriteImage[] favorite)
        {
            //create a image object containing the photograph to watermark
            Image imgPhoto = Image.FromFile(body_path);
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            //create a Bitmap the Size of the original photograph
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            //设置此 Bitmap 的分辨率。 
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            //load the Bitmap into a Graphics object 
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            //Set the rendering quality for this Graphics object
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
 //haix
            for (int i = 0; i < favorite.Length; i++)
            {  
            //Draws the photo Image object at original size to the graphics object.
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw. 
                0,                                      // y-coordinate of the portion of the source image to draw. 
                phWidth,                                // Width of the portion of the source image to draw. 
                phHeight,                               // Height of the portion of the source image to draw. 
                GraphicsUnit.Pixel);                    // Units of measure 

             
                //------------------------------------------------------------
                //Step #2 - Insert Property image,For example:hair,skirt,shoes etc.
                //------------------------------------------------------------
                //create a image object containing the watermark
                Image imgWatermark = new Bitmap(favorite[i].imagePath);
                int wmWidth = imgWatermark.Width;
                int wmHeight = imgWatermark.Height;
                

                //Create a Bitmap based on the previously modified photograph Bitmap
                Bitmap bmWatermark = new Bitmap(bmPhoto);
                bmWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。

                //bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
                //Load this Bitmap into a new Graphic Object
                Graphics grWatermark = Graphics.FromImage(bmWatermark);


                int xPosOfWm = favorite[i].x;
                int yPosOfWm = favorite[i].y;

                //叠加
                grWatermark.DrawImage(imgWatermark,new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position
                0,                  // x-coordinate of the portion of the source image to draw. 
                0,                  // y-coordinate of the portion of the source image to draw. 
                wmWidth,            // Watermark Width
                wmHeight,            // Watermark Height
                GraphicsUnit.Pixel, // Unit of measurment
                null);   //ImageAttributes Object


                //Replace the original photgraphs bitmap with the new Bitmap
                imgPhoto = bmWatermark;

                //grWatermark.Dispose();
                //imgWatermark.Dispose();
                //grPhoto.Dispose();                
                //bmWatermark.Dispose();
            }
            //haix

            string nowTime = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString();
            nowTime += DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

            string saveImagePath = savePath + "//FA" + nowTime + ".png";
            
            //save new image to file system.
            imgPhoto.Save(saveImagePath, ImageFormat.Png);            
            imgPhoto.Dispose();
        

            return saveImagePath;
        }
    }
}

叠加后的图片:

一个body,一个hair,一个西服:

C# 多个图片叠加,图片透明.相关推荐

  1. java 操作图片 叠加图片 覆盖

    需求说明, 需要处理一个pdf文件, 将ocr识别出来的文字, 添加到word中, 将图片作为word背景图片, 也就实现了pdf转word功能. import java.awt.AlphaCompo ...

  2. Python PIL Image 4通道透明图片叠加(ARGB)

    安装Package pip install Pillow pip install numpy RGBA与RGB的区别 一般图片(Jpeg)的呈现都是以3通道RGB显示出来 from PIL impor ...

  3. PNG透明图片叠加(Opencv实现,包括旋转缩放以及边界处理)

    叠加效果 原图 叠加图片 大致实现了一个四通道的PNG矢量图叠加到一个三通道的图片上,函数如下: bool 叠加(cv::Mat &dst, cv::Mat &src, double ...

  4. php 网页 透明背景图片,php 处理透明背景的图片时的问题

    PHP图象处理之透明背景的gif和png图片的一些问题 1,直接读取有透明背景的PNG格式文件,然后直接输出,背景变成了黑色,gif则没有这种情况. 解决方法:使用 imagesavealpha ($ ...

  5. Android的图片叠加

    android.graphics.Bitmap.Config是一个枚举类型,里面定义了位图的四种格式.   ALPHA_8,ARGB_4444,ARGB_8888都是透明的位图,也就是所字母A代表透明 ...

  6. Java应用:图片叠加

    图片叠加 一. 功能目标 二. 代码实现 2.1 依赖 2.2 图片叠加服务 一. 功能目标 现有一张背景图片,一张水印图片,希望将水印图片叠加到背景图片上方的指定位置. 本代码基于Spring bo ...

  7. c++调用opencv实现图片叠加以及添加水印效果

    前言 本篇文章参考了以下文章: https://blog.csdn.net/mars_xiaolei/article/details/78791133 https://answers.opencv.o ...

  8. 两个图片叠加在一起css,css实现图片叠加的几种思路(记录笔记)

    3.使用div层叠,设置两个div,一个position:relative;一个position:absolute; 就可以实现在div里面显示绝对定位,而不跑出div的层叠div,对于没有固定整个界 ...

  9. CSS3初级学习(二)背景图片叠加

    CSS3初级学习(二) 背景图片叠加 Hello!大家好久不见,这里是学业日渐繁忙的万物之恋,沉迷学习无心写博客(并不是),久违的来和大家分享下自己在学习过程中碰到的一些有趣案列.话不多说: 先看效果 ...

最新文章

  1. 2020年牛客算法入门课练习赛1【完结】
  2. 技术系列课|AI驱动的超分辨技术应用现状
  3. 编译安装php的openssl组件时遇到 libssl not found解决办法
  4. IDEA 运行spingboot时出现Process finished with exit code -1073741819 (0xC0000005)
  5. 突然!新锤子手机系统现身:这是另起炉灶了?
  6. Flask 系列之 SQLAlchemy
  7. Codejock Suite Pro ActiveX 20.0 Crack
  8. revi插件使用方法| 如何在Revit中修改CAD图纸
  9. Pytorch Note19 优化算法5 Adadelta算法
  10. 游戏模型与动漫之间,有多大差距?
  11. C++计算md5/sha1/sha256/sha384/sha512算法
  12. 趣图:程序员的斗志曲线图
  13. matlab里面floor是什么意思,matlab中的fix、round、floor、ceil的区别与含义
  14. MPLS LDP原理
  15. 阿里云服务器优惠活动 降低至7.7元/月
  16. 2014年传智Itcast C++培训3期视频全套
  17. VS2022编写Qt遇到的问题之 E1391 无法定义 dllimport 实体
  18. Semantic Sentence Matching with Densely-connected Recurrent and Co-attentive Information
  19. Spring框架对redis的封装
  20. python基础面试题

热门文章

  1. 力扣——罗马数字转整数
  2. 实验2-2-4 计算分段函数[2] (10 分)
  3. PCL最小二乘法进行平面拟合原理
  4. .net中button按钮点击之后的两个参数是什么意思_如果想完成更加有意思和强大的程序,你应该这么做...
  5. 【必收藏】台大李宏毅老师课程 | 资源汇总、笔记总结与索引
  6. Python3 有序字典—OrderedDict()
  7. 不使用border-radius,实现一个可复用的高度和宽度都自适应的圆角矩形
  8. 暴力 ZOJ 1403 Safecracker
  9. sv_labs学习笔记 专栏说明博文目录
  10. rabbitmq python 发送失败_python rabbitmq no_ack=false