下载该示例代码
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,一个西服:
kaixin110 是一位Web工程师。他原来是在郑州一家软件公司做WebGIS (JSP+Servlet+Tomcat),对WebGIS的原理有一定研究,在狂热地编写代码之余,他喜欢打篮球并体验软件带给我们的惊喜。目前在中国深圳一公司做Web开发(ASP.NET),如果您希望就本文与 kaixin110联系,则可以通过kaixin110@gmail.com与他联系。