C# 在c中,一个图像与另一个图像重叠为透明#

C# 在c中,一个图像与另一个图像重叠为透明#,c#,winforms,overlay,transparency,C#,Winforms,Overlay,Transparency,我正在根据给定图像的凝视区域创建热图。我可以创建热图并将其作为新文件保存到我的桌面。但是我想创建另一个图像,它由热图和用来收集眼球跟踪数据的图像组成。我希望原始图片是坚实的背景,然后重叠(或覆盖我不知道)作为透明的热图,并合并成一个新的文件。有没有办法用c#来做这件事? 谢谢好的,我找到了答案 首先,我调整我的背景图片 public static Bitmap ResizeImage(Bitmap image, int width, int height) { var d

我正在根据给定图像的凝视区域创建热图。我可以创建热图并将其作为新文件保存到我的桌面。但是我想创建另一个图像,它由热图和用来收集眼球跟踪数据的图像组成。我希望原始图片是坚实的背景,然后重叠(或覆盖我不知道)作为透明的热图,并合并成一个新的文件。有没有办法用c#来做这件事? 谢谢

好的,我找到了答案

首先,我调整我的背景图片

public static Bitmap ResizeImage(Bitmap image, int width, int height)
    {
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }
        return destImage;
    }
之后,必须将热图设置为另一个不透明度级别,以使背景可见

public static Image SetImageOpacity(Image image, float opacity)
    {
        try
        {
            //create a Bitmap the size of the image provided  
            Bitmap bmp = new Bitmap(image.Width, image.Height);

            //create a graphics object from the image  
            using (Graphics gfx = Graphics.FromImage(bmp))
            {

                //create a color matrix object  
                ColorMatrix matrix = new ColorMatrix();

                //set the opacity  
                matrix.Matrix33 = opacity;

                //create image attributes  
                ImageAttributes attributes = new ImageAttributes();

                //set the color(opacity) of the image  
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                //now draw the image  
                gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
            }
            return bmp;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            throw ex;
            //return null;
        }
    } 
最后,我们需要将这两者合并

        public static Image Overlap(Image source1, Image 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);

        return target;
    }
以下是我的背景:

这是合并后的版本,有眼球追踪数据的热图

试试下面的答案这是用来将两个图像合并成一个图像并保存结果文件的。嗯……嗯,你好像没怎么试过。。。基本上,您需要覆盖控件的
OnPaint()
方法,以便在其中绘制合成图像。查看我在谷歌搜索
c#image layers
一秒钟后找到的这个链接。例如,谢谢,让我用图片盒检查一下。显示一个作为背景图像,另一个作为图像。要合并,请使用DrawToBitmap!。当然,这取决于你使覆盖半透明!我还建议大家看看如何创建一个热图梯度,这将使您的数据看起来更好