如何从另一个tiff图像向C#中的tiff添加额外通道?

如何从另一个tiff图像向C#中的tiff添加额外通道?,c#,image-processing,tiff,C#,Image Processing,Tiff,我需要从另一个灰度tiff向tiff图像添加一个额外通道(专色perphaps)。结果图像的通道必须在Photoshop中看到,如下所示: 这是我试图使用的代码: static public void getGrayOverlayLBA(Bitmap bmp1, Bitmap bmp2) { Size s1 = bmp1.Size; Size s2 = bmp2.Size; if (s1 != s2) return; P

我需要从另一个灰度tiff向tiff图像添加一个额外通道(专色perphaps)。结果图像的通道必须在Photoshop中看到,如下所示:

这是我试图使用的代码:

static public void getGrayOverlayLBA(Bitmap bmp1, Bitmap bmp2)
    {
        Size s1 = bmp1.Size;
        Size s2 = bmp2.Size;
        if (s1 != s2) return;

        PixelFormat fmt1 = bmp1.PixelFormat;
        PixelFormat fmt2 = bmp2.PixelFormat;

        PixelFormat fmt = new PixelFormat();
        fmt = PixelFormat.Format32bppArgb;
        Bitmap bmp3 = new Bitmap(s1.Width, s1.Height, fmt);

        Rectangle rect = new Rectangle(0, 0, s1.Width, s1.Height);

        BitmapData bmp1Data = bmp1.LockBits(rect, ImageLockMode.ReadOnly, fmt1);
        BitmapData bmp2Data = bmp2.LockBits(rect, ImageLockMode.ReadOnly, fmt2);
        BitmapData bmp3Data = bmp3.LockBits(rect, ImageLockMode.ReadWrite, fmt);

        byte bpp1 = 4;
        byte bpp2 = 4;
        byte bpp3 = 4;

        if (fmt1 == PixelFormat.Format24bppRgb) bpp1 = 3;
        else if (fmt1 == PixelFormat.Format32bppArgb || fmt1 == PixelFormat.Format32bppPArgb) bpp1 = 4; else return;
        if (fmt2 == PixelFormat.Format24bppRgb) bpp2 = 3;
        else if (fmt2 == PixelFormat.Format32bppArgb) bpp2 = 4; else return;

        int size1 = bmp1Data.Stride * bmp1Data.Height;
        int size2 = bmp2Data.Stride * bmp2Data.Height;
        int size3 = bmp3Data.Stride * bmp3Data.Height;
        byte[] data1 = new byte[size1];
        byte[] data2 = new byte[size2];
        byte[] data3 = new byte[size3];
        System.Runtime.InteropServices.Marshal.Copy(bmp1Data.Scan0, data1, 0, size1);
        System.Runtime.InteropServices.Marshal.Copy(bmp2Data.Scan0, data2, 0, size2);
        System.Runtime.InteropServices.Marshal.Copy(bmp3Data.Scan0, data3, 0, size3);

        for (int y = 0; y < s1.Height; y++)
        {
            for (int x = 0; x < s1.Width; x++)
            {
                int index1 = y * bmp1Data.Stride + x * bpp1;
                int index2 = y * bmp2Data.Stride + x * bpp2;
                int index3 = y * bmp3Data.Stride + x * bpp3;
                Color c1, c2;

                if (bpp1 == 4)
                    c1 = Color.FromArgb(data1[index1 + 3], data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
                else c1 = Color.FromArgb(255, data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
                if (bpp2 == 4)
                    c2 = Color.FromArgb(data2[index2 + 3], data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);
                else c2 = Color.FromArgb(255, data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);

                byte A = (byte)(255 * c2.GetBrightness());
                data3[index3 + 0] = c1.B;
                data3[index3 + 1] = c1.G;
                data3[index3 + 2] = c1.R;
                data3[index3 + 3] = A;
            }
        }

        System.Runtime.InteropServices.Marshal.Copy(data3, 0, bmp3Data.Scan0, data3.Length);
        bmp1.UnlockBits(bmp1Data);
        bmp2.UnlockBits(bmp2Data);
        bmp3.UnlockBits(bmp3Data);
        if (File.Exists("result.tiff"))
            File.Delete("result.tiff");
        bmp3.Save("result.tif", ImageFormat.Tiff);
    }
static public void getGrayOverlayLBA(位图bmp1、位图bmp2)
{
尺寸s1=bmp1。尺寸;
尺寸s2=bmp2。尺寸;
如果(s1!=s2)返回;
PixelFormat fmt1=bmp1.PixelFormat;
PixelFormat fmt2=bmp2.PixelFormat;
PixelFormat fmt=新PixelFormat();
fmt=PixelFormat.Format32bppArgb;
位图bmp3=新位图(s1.宽度,s1.高度,fmt);
矩形rect=新矩形(0,0,s1.宽度,s1.高度);
BitmapData bmp1Data=bmp1.LockBits(rect、ImageLockMode.ReadOnly、fmt1);
BitmapData bmp2Data=bmp2.LockBits(rect、ImageLockMode.ReadOnly、fmt2);
BitmapData bmp3Data=bmp3.LockBits(rect、ImageLockMode.ReadWrite、fmt);
字节bpp1=4;
字节bpp2=4;
字节bpp3=4;
如果(fmt1==PixelFormat.Format24bppRgb)bpp1=3;
else if(fmt1==PixelFormat.Format32bppArgb | | fmt1==PixelFormat.Format32bppPArgb)bpp1=4;else返回;
如果(fmt2==PixelFormat.Format24bppRgb)bpp2=3;
else if(fmt2==PixelFormat.Format32bppArgb)bpp2=4;else返回;
int size1=bmp1Data.Stride*bmp1Data.Height;
int size2=bmp2Data.Stride*bmp2Data.Height;
int size3=bmp3Data.Stride*bmp3Data.Height;
字节[]数据1=新字节[size1];
字节[]数据2=新字节[size2];
字节[]数据3=新字节[size3];
System.Runtime.InteropServices.Marshal.Copy(bmp1Data.Scan0,data1,0,size1);
System.Runtime.InteropServices.Marshal.Copy(bmp2Data.Scan0,data2,0,size2);
System.Runtime.InteropServices.Marshal.Copy(bmp3Data.Scan0,data3,0,size3);
对于(int y=0;y
但是我的结果图像在整个图像上有奇怪的透明度,并且没有预期的额外通道。两张图片大小相同。我做错了什么?有没有免费的C#图书馆可以做到这一点

预期结果:

我的结果是:

UPD。添加了源文件

来源tiff:

要作为额外通道添加到源中的灰度tiff:


UPD2。更新了预期结果图像。

Uh。你确定你指的是“频道”吗?通道是由彩色组件构建图像的方式的内在结果。如果你有一个红色、绿色和蓝色通道,并且你添加了第四个,那么是的,第四个将是唯一剩下的选项。。。Alpha通道,为图像增加透明度。该代码中的变量甚至明确表示为“A”,颜色格式明确地从RGB升级为ARGB。您显示结果,但您的输入是什么?因为我不认为你想做的事与频道有任何关系。@nyrguds我需要用描述的方式来做。但我对alpha通道或spot通道的定义感到困惑。据我所知,您的预期结果没有alpha通道。那么为什么要添加alpha通道呢?正如我所说,alpha通道=透明度。或者,更确切地说,不透明,真的:阿尔法越低,东西就越透明。“灰度tiff添加为通道”不是一个通道;它是亮度信息和alpha;它有自己的透明度,在编辑器中可见。@nyrguds那么在第一个名为“WHITE_INK”的屏幕截图上,第五层叫什么呢?它可以是专色频道还是其他额外频道?我需要从灰度tiff将其添加到源图像中。您提到的是“点通道”。这与图像的实际保存方式无关。Photoshop只是喜欢将所有内容分割成灰度“通道”,即使它们不是最终图像文件中的真实通道。不过,您在这里没有提供足够的信息来确切了解这种“现场频道”的功能,以及如何在c#中重现它。你确定你指的是“频道”吗?通道是由彩色组件构建图像的方式的内在结果。如果你有一个红色、绿色和蓝色通道,并且你添加了第四个,那么是的,第四个将是唯一剩下的选项。。。Alpha通道,为图像增加透明度。该代码中的变量甚至明确表示为“A”,颜色格式明确地从RGB升级为ARGB。您显示结果,但您的输入是什么?因为我不认为你想做的与频道有任何关系。@nyrguds我需要在d中做