合并两个具有透明度的 png 图像并保留透明度

发布于 2024-11-26 07:37:06 字数 367 浏览 1 评论 0原文

可能的重复:
在 C#/.NET 中合并两个图像

我有两个 png 格式的图像,并且都有定义了透明度。我需要将它们合并成一个新的 png 图像,但不会丢失结果的任何透明度。将第一个图像视为主图像,第二个图像用于添加叠加层,例如添加/编辑/删除指示器。我正在尝试创建一个小实用程序,它将获取主图像和一组叠加层,然后生成组合它们的结果输出图像集。

对于 PHP 的解决方案似乎有很多答案,但对于 C#/ 却没有。

Possible Duplicate:
Merging two images in C#/.NET

I have two png format images and both have transparency defined. I need to merge these together into a new png image but without losing any of the transparency in the result. Think of the first image as the main image and the second is used to add an overlay, such as a add/edit/delete indicator. I am trying to create a little utility that will take a main image and a set of overlays and then generate the resultant set of output images that combine them.

There seem to be plenty of answers with solutions for PHP but nothing for C#/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你鎻心 2024-12-03 07:37:07

这应该有效。

Bitmap source1; // your source images - assuming they're the same size
Bitmap source2;
var target = new Bitmap(source1.Width, source1.Height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(target);
graphics.CompositingMode = CompositingMode.SourceOver; // this is the default, but just to be clear

graphics.DrawImage(source1, 0, 0);
graphics.DrawImage(source2, 0, 0);

target.Save("filename.png", ImageFormat.Png);

This should work.

Bitmap source1; // your source images - assuming they're the same size
Bitmap source2;
var target = new Bitmap(source1.Width, source1.Height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(target);
graphics.CompositingMode = CompositingMode.SourceOver; // this is the default, but just to be clear

graphics.DrawImage(source1, 0, 0);
graphics.DrawImage(source2, 0, 0);

target.Save("filename.png", ImageFormat.Png);
随遇而安 2024-12-03 07:37:07

不幸的是,您没有提到如何获取像素,

因此 p 代码:

// The result will have its alpha chanell from "first", 
// the color channells from "second".

assert (first.width = second.width)
assert (first.height = second.height)

for y in 0..height
    for x in 0..width
        RGBA col_first  = first(x,y)
        RGBA col_second = second(x,y)

        result(x,y) =  RGBA(col_second.r,
                            col_second.g,
                            col_second.b,
                            col_first.a  ))

Unfortunately you haven't mentioned how you get the pixels,

so p-code:

// The result will have its alpha chanell from "first", 
// the color channells from "second".

assert (first.width = second.width)
assert (first.height = second.height)

for y in 0..height
    for x in 0..width
        RGBA col_first  = first(x,y)
        RGBA col_second = second(x,y)

        result(x,y) =  RGBA(col_second.r,
                            col_second.g,
                            col_second.b,
                            col_first.a  ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文