我希望能够将一个图像放置在另一个图像上,但是为覆盖的图像应用特定的透明度级别。
这是我目前为止写的:
private static Image PlaceImageOverImage(Image background, Image overlay, int x, int y, int alpha)
{
using (Graphics graphics = Graphics.FromImage(background))
{
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.DrawImage(overlay, new Point(x, y));
}
return background;
}
你可以使用ColorMatrix:
private static Image PlaceImageOverImage(Image background, Image overlay, int x, int y, float alpha)
{
using (Graphics graphics = Graphics.FromImage(background))
{
var cm = new ColorMatrix();
cm.Matrix33 = alpha;
var ia = new ImageAttributes();
ia.SetColorMatrix(cm);
graphics.DrawImage(
overlay,
// target
new Rectangle(x, y, overlay.Width, overlay.Height),
// source
0, 0, overlay.Width, overlay.Height,
GraphicsUnit.Pixel,
ia);
}
return background;
}
注意:alpha是浮点数(0…1)
PS:我宁愿创建一个新的位图并返回它,而不是改变现有的位图。>>> 这是关于函数式编程的