使用特定的 alpha 透明度级别在 C# 中将图像放置在图像上
Place image over image in C# using specific alpha transparency level
我希望能够将图像放置在图像之上,但对叠加图像应用特定级别的透明度。
这是我目前拥有的:
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: 我宁愿创建一个新的位图并 return 它,而不是改变现有的位图。 (还有 return 它) >>> 它是关于函数式编程的。
我希望能够将图像放置在图像之上,但对叠加图像应用特定级别的透明度。
这是我目前拥有的:
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: 我宁愿创建一个新的位图并 return 它,而不是改变现有的位图。 (还有 return 它) >>> 它是关于函数式编程的。