如何区分2张图像并将其保存到图像中 [英] How to get difference between 2 images and save it to an image

查看:128
本文介绍了如何区分2张图像并将其保存到图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是标题是
我有2张图片 - 每张图片来自不同的时间(来自屏幕上限)
i希望得到两个
之间的差异make all相同的部分透明,
将差额发送给客户
并将其放在他拥有的图像之上

And what i mean by the title is I have 2 images - each one is from a different time ( from screen cap) i want to get the difference between the two make all the same parts transparent, send the difference to a client and place it on top of an image that he has

不要担心发送部分 - 我已经介绍过了。
我需要帮助的是如何获得差异,将其保存到图像/流/字节数组
并将其合并到一个图像。

Don't worry about the sending part - I have covered that. The thing I need help is how to get the difference, save it to an image / stream / byte array and merging it to one image.

推荐答案

您创建并填充目标位图

如果性能太差,请尝试使用 LockBits
使用库应该更快。

Do try to use LockBits if performance is too bad. Using a library should be even faster.

这是一段快速的代码;你传入两个位图颜色,应该涂上差异,例如: Color.Red Color.Transparent

Here is a quick piece of code; you pass in two Bitmaps and the Color the differences should be painted in, e.g. Color.Red or Color.Transparent.

It返回差值位图,如果位图大小不匹配,则返回null。

It returns the difference Bitmap or null if the Bitmap Sizes don't match.

public Bitmap getDifferencBitmap(Bitmap bmp1, Bitmap bmp2, Color diffColor)
{
    Size s1 = bmp1.Size;
    Size s2 = bmp2.Size;
    if (s1 != s2) return null;

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

    for (int y = 0; y < s1.Height; y++)
        for (int x = 0; x < s1.Width; x++)
        {
            Color c1 =    bmp1.GetPixel(x, y);
            Color c2 =    bmp2.GetPixel(x, y);
            if (c1 == c2) bmp3.SetPixel(x, y, c1);
            else          bmp3.SetPixel(x, y, diffColor);
        }
    return bmp3;
}

您可以这样称呼:

Bitmap bmp1 = new Bitmap(filepath1);
Bitmap bmp2 = new Bitmap(filepath2);
Bitmap bmp3 = getDifferencBitmap(bmp1, bmp2, Color.Transparent);

bmp3.Save(filepath3, System.Drawing.Imaging.ImageFormat.Png);

bmp1.Dispose();
bmp2.Dispose();
bmp3.Dispose();

确保在处理位图时你已经完成了它们!

Make sure you dispose of the Bitmaps when you are done with them!

当然你可以轻松改变逻辑来制作相同的零件透明和不相同的零件无论如何......我认为你真的想要展示但是差异很大。

Of course you can easily change the logic to make identical parts Transparent and non-identical parts whatever.. I assumed you really wanted to show the differences, though.

这里是一个帖子,其中包含LockBits版本的代码;你只需要将内循环的代码更改为:

Here is a post which contains the code for a LockBits version; you just would need to change the inner loop's code to this:

    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 (bpp1 == 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]);
        Color putColor = (c1 == c2 ? c1 : diffColor);

        data3[index3 + 0] = putColor.B;
        data3[index3 + 1] = putColor.G;
        data3[index3 + 2] = putColor.R;
        data3[index3 + 3] = putColor.A;
    }

这篇关于如何区分2张图像并将其保存到图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆