C# 多个图片叠加,图片透明.

下载该示例代码

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
using  System.Drawing;
None.gif
using  System.Drawing.Imaging;
None.gif
using  System.Drawing.Drawing2D;
None.gif
None.gif
namespace  Haix.Utils
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class GenerateImage
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
public struct favoriteImage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
private string _imagePath;            
InBlock.gif            
private int _x;
InBlock.gif            
private int _y;
InBlock.gif            
InBlock.gif            
public int x
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return _x; 
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _x  
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
public int y
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return _y;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _y 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
public string imagePath
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
get
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return _imagePath;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
set
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _imagePath 
= value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
        
InBlock.gif
InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string CurrentDirectory = System.Environment.CurrentDirectory;
InBlock.gif            
string body_path=CurrentDirectory + "\\white.png"
InBlock.gif
InBlock.gif            favoriteImage[] FaImage 
= new favoriteImage[2];
InBlock.gif            
InBlock.gif            FaImage[
0].x = -3;
InBlock.gif            FaImage[
0].y = 70;
InBlock.gif            FaImage[
0].imagePath = CurrentDirectory + "\\1.png";
InBlock.gif
InBlock.gif            FaImage[
1].x = 20;//65;
InBlock.gif
            FaImage[1].y = -12;
InBlock.gif            FaImage[
1].imagePath = CurrentDirectory + "\\2.png";
InBlock.gif
InBlock.gif            generateWinterMark(CurrentDirectory,body_path, FaImage);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 生成水印
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Main">主图片路径,eg:body</param>
InBlock.gif        
/// <param name="Child">要叠加的图片路径</param>
InBlock.gif        
/// <param name="x">要叠加的图片位置的X坐标</param>
InBlock.gif        
/// <param name="y">要叠加的图片位置的Y坐标</param>
InBlock.gif        
/// <param name="isSave"></param>
ExpandedSubBlockEnd.gif        
/// <returns>生成图片的路径</returns>        

InBlock.gif        private static string generateWinterMark(string savePath,string body_path,favoriteImage[] favorite)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a image object containing the photograph to watermark
InBlock.gif
            Image imgPhoto = Image.FromFile(body_path);
InBlock.gif            
int phWidth = imgPhoto.Width;
InBlock.gif            
int phHeight = imgPhoto.Height;
InBlock.gif
InBlock.gif            
//create a Bitmap the Size of the original photograph
InBlock.gif
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
InBlock.gif
InBlock.gif            
//设置此 Bitmap 的分辨率。 
InBlock.gif
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
InBlock.gif
InBlock.gif            
//load the Bitmap into a Graphics object 
InBlock.gif
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
InBlock.gif            
//Set the rendering quality for this Graphics object
InBlock.gif
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;//清除锯齿的呈现
InBlock.gif 
//haix
InBlock.gif
            for (int i = 0; i < favorite.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  
InBlock.gif            
//Draws the photo Image object at original size to the graphics object.
InBlock.gif
            grPhoto.DrawImage(
InBlock.gif                imgPhoto,                               
// Photo Image object
InBlock.gif
                new Rectangle(00, phWidth, phHeight), // Rectangle structure
InBlock.gif
                0,                                      // x-coordinate of the portion of the source image to draw. 
InBlock.gif
                0,                                      // y-coordinate of the portion of the source image to draw. 
InBlock.gif
                phWidth,                                // Width of the portion of the source image to draw. 
InBlock.gif
                phHeight,                               // Height of the portion of the source image to draw. 
InBlock.gif
                GraphicsUnit.Pixel);                    // Units of measure 
InBlock.gif
InBlock.gif             
InBlock.gif                
//------------------------------------------------------------
InBlock.gif                
//Step #2 - Insert Property image,For example:hair,skirt,shoes etc.
InBlock.gif                
//------------------------------------------------------------
InBlock.gif                
//create a image object containing the watermark
InBlock.gif
                Image imgWatermark = new Bitmap(favorite[i].imagePath);
InBlock.gif                
int wmWidth = imgWatermark.Width;
InBlock.gif                
int wmHeight = imgWatermark.Height;
InBlock.gif                
InBlock.gif
InBlock.gif                
//Create a Bitmap based on the previously modified photograph Bitmap
InBlock.gif
                Bitmap bmWatermark = new Bitmap(bmPhoto);
InBlock.gif                bmWatermark.MakeTransparent(); 
//使默认的透明颜色对此 Bitmap 透明。
InBlock.gif
InBlock.gif                
//bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
InBlock.gif                
//Load this Bitmap into a new Graphic Object
InBlock.gif
                Graphics grWatermark = Graphics.FromImage(bmWatermark);
InBlock.gif
InBlock.gif
InBlock.gif                
int xPosOfWm = favorite[i].x;
InBlock.gif                
int yPosOfWm = favorite[i].y;
InBlock.gif
InBlock.gif                
//叠加
InBlock.gif
                grWatermark.DrawImage(imgWatermark,new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //Set the detination Position
InBlock.gif
                0,                  // x-coordinate of the portion of the source image to draw. 
InBlock.gif
                0,                  // y-coordinate of the portion of the source image to draw. 
InBlock.gif
                wmWidth,            // Watermark Width
InBlock.gif
                wmHeight,            // Watermark Height
InBlock.gif
                GraphicsUnit.Pixel, // Unit of measurment
InBlock.gif
                null);   //ImageAttributes Object
InBlock.gif
InBlock.gif
InBlock.gif                
//Replace the original photgraphs bitmap with the new Bitmap
InBlock.gif
                imgPhoto = bmWatermark;
InBlock.gif
InBlock.gif                
//grWatermark.Dispose();
InBlock.gif                
//imgWatermark.Dispose();
InBlock.gif                
//grPhoto.Dispose();                
InBlock.gif                
//bmWatermark.Dispose();
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
//haix
InBlock.gif

InBlock.gif            
string nowTime = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString();
InBlock.gif            nowTime 
+= DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
InBlock.gif
InBlock.gif            
string saveImagePath = savePath + "\\FA" + nowTime + ".png";
InBlock.gif            
InBlock.gif            
//save new image to file system.
InBlock.gif
            imgPhoto.Save(saveImagePath, ImageFormat.Png);            
InBlock.gif            imgPhoto.Dispose();
InBlock.gif        
InBlock.gif
InBlock.gif            
return saveImagePath;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


叠加后的图片:

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

FA2007847313.png



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

转载于:https://www.cnblogs.com/kaixin110/archive/2007/08/03/841827.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值