请教:C#中图像叠加?

sdl2005lyx 2007-02-12 05:22:01
在MFC中,CImageList类的SetOverlayImage方法可以使任意两个图像叠加成一个新的图像,或VB/VC的ImageList 控件中Overlay 方法具备同样功能,但我怎么也没找到C#中对应的方法,有谁知道,请告知,先谢过!!
...全文
1052 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdl2005lyx 2007-02-13
  • 打赏
  • 举报
回复
谢谢各位,我的叠加意思:两个图像叠加成一个新的图像,原来的两个图像保持不变!
viena(维也纳nn) ( ) 提供的办法,我马上试试。。。
jinglong6511 2007-02-13
  • 打赏
  • 举报
回复
学习
rivus 2007-02-13
  • 打赏
  • 举报
回复
mark
viena 2007-02-13
  • 打赏
  • 举报
回复
是那个经典的水印源码,注释是我汉化的
viena 2007-02-13
  • 打赏
  • 举报
回复
参考如下加半透明图片水印的方法,透明度可调


/// <summary>
/// 加水印,对传入的Image对象操作
/// </summary>
/// <param name="watermarkFullPath">水印图片的完整路径</param>
/// <param name="imgPhoto">要加水印的Bitmap对象,直接在上面加水印(ref 引用传递)</param>
public static void AddWaterMark(string watermarkFullPath,ref Bitmap imgPhoto)
{
//string Copyright="Copyright ? 2005 - 2006 Lotour.com";

int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//创建一个与原图尺寸相同的位图
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//位图装载到一个Graphics对象
Graphics grPhoto = Graphics.FromImage(bmPhoto);

//用水印BMP文件创建一个image对象
Image imgWatermark = new Bitmap(watermarkFullPath);
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;

//设置图片质量
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//以原始尺寸把照片图像画到此graphics对象
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

//基于前面已修改的Bitmap创建一个新Bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Load this Bitmap into a new Graphic Object
Graphics grWatermark = Graphics.FromImage(bmWatermark);

//To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes();

//第一步是以透明色(Alpha=0, R=0, G=0, B=0)来替换背景色
//为此我们将使用一个Colormap并用它来定义一个RemapTable
ColorMap colorMap = new ColorMap();

//水印被定义为一个100%的绿色背景l
//这将是我们以transparency来查找并替换的颜色
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

ColorMap[] remapTable = {colorMap};

imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

//第二个颜色操作是用来改变水印的透明度
//用包涵the coordinates for the RGBA space的一个5x5 的矩阵
//设置第三行第三列to 0.3f we achive a level of opacity
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.8f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);

//水印放在图像的右上角
//向下10像素,向左10像素
int xPosOfWm = ((phWidth - wmWidth)-10);
int yPosOfWm = 10;

grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight), //Set the detination Position
0, // 源图的横坐标位置
0, // 源图的纵坐标位置
wmWidth, // 水印宽度
wmHeight, // 水印高度
GraphicsUnit.Pixel, // Unit of measurment
imageAttributes); //ImageAttributes Object

//以新图替换原始图
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
imgWatermark.Dispose();
}

/// <summary>
/// 加水印(保存目标Stream)
/// </summary>
/// <param name="watermarkFullPath">水印图片的完整路径</param>
/// <param name="imgPhoto">要加水印的Bitmap对象</param>
/// <param name="destStream">加了水印的图片Stream</param>
public static void AddWaterMark(string watermarkFullPath,ref Bitmap imgPhoto,Stream destStream)
{
//加水印
AddWaterMark(watermarkFullPath,ref imgPhoto);

//保存加了水印的图片到Stream
imgPhoto.Save(destStream, ImageFormat.Jpeg);
}

/// <summary>
/// 加水印(直接保存目标文件)
/// </summary>
/// <param name="watermarkFullPath">水印图片的完整路径</param>
/// <param name="imgPhoto">要加水印的Bitmap对象</param>
/// <param name="destFile">加了水印的图片保存路径</param>
public static void AddWaterMark(string watermarkFullPath,ref Bitmap imgPhoto,string destFile)
{
//加水印
AddWaterMark(watermarkFullPath,ref imgPhoto);

//保存加了水印的图片
imgPhoto.Save(destFile, ImageFormat.Jpeg);
}
王集鹄 2007-02-13
  • 打赏
  • 举报
回复
搂主,你所说的图像叠加是不是指把一张图片绘制到另一张图片上?
Graphics.DrawImage()方法就可以了
如果是半透明绘制还得用其他方法
你先说清楚“叠加”是啥意思?
sdl2005lyx 2007-02-13
  • 打赏
  • 举报
回复
zswang(伴水清清)(专家门诊清洁工) ,昨晚试了,不行,还有没有其他办法?
jxf654 2007-02-13
  • 打赏
  • 举报
回复
up
best8625 2007-02-13
  • 打赏
  • 举报
回复
Up..
jimh 2007-02-13
  • 打赏
  • 举报
回复
怎么样个叠加法?大家半透明叠加还是变成一个image两张图片层?
my_infinity 2007-02-13
  • 打赏
  • 举报
回复
偶要mark 下!!!!!
sdl2005lyx 2007-02-12
  • 打赏
  • 举报
回复
zswang(伴水清清)(专家门诊清洁工) ,谢谢了,我试试。。。
jxf654 2007-02-12
  • 打赏
  • 举报
回复
up
王集鹄 2007-02-12
  • 打赏
  • 举报
回复
Bitmap vBitmapBack = new Bitmap(@"c:\temp\temp.bmp");
Bitmap vBitmapFace = new Bitmap(@"c:\temp\1.bmp");
Graphics vGraphics = Graphics.FromImage(vBitmapBack);
vBitmapFace.MakeTransparent(Color.Fuchsia);
vGraphics.DrawImage(vBitmapFace, new Point(10, 10));
vBitmapBack.Save(@"c:\temp\2.bmp");

//这样?
asuan 2007-02-12
  • 打赏
  • 举报
回复
没用过。

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧