C# 两张图片像素比较,取出差异像素,效率甩图片压缩N+1倍

最近在做远程控制项目,发现图片传输速度慢到失眠,各种压缩都试了不行,最后发现这个差异化传输,

原理就是把两张图片不同的像素取出来并生成一张只包含差异像素的图片。

现在功能实现了,实际效果要等接入项目后再看了,希望有用。

话不多说,上码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string file = textBox1.Text;//原图片位置
            Bitmap bmp = new Bitmap(Image.FromFile(file));
            LockBitmap lmp = new LockBitmap(bmp);
            lmp.LockBits();//锁定bitmap到内存

            Bitmap bmp2 = getScreen();//新图片采用截图获取
            LockBitmap lmp2 = new LockBitmap(bmp2);
            lmp2.LockBits();//锁定bitmap到内存

            Bitmap bmp3 = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//建立空白图片,以便存储差异像素
            LockBitmap lmp3 = new LockBitmap(bmp3);
            lmp3.LockBits();//锁定bitmap到内存
            int w = bmp.Width;
            int h = bmp.Height;

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    //获取像素随便赋值给q  
                    Color c1 = lmp.LockGetPixel(i,j);
                    Color c2 = lmp2.LockGetPixel(i, j);
                    if (c1 != c2)
                    {
                        lmp3.LockSetPixel(i,j,c2);
                    }
                }
            }
            lmp.UnlockBits();//解除锁定
            lmp2.UnlockBits();//解除锁定
            lmp3.UnlockBits();//解除锁定
            bmp3.Save("aa.png");//存储最后比对的差异图片,背景透明的png文件
            MessageBox.Show("complete");

        }

        /// 
        /// 截全屏并保存成Bitmap
        /// 
        private Bitmap getScreen()
        {
            try
            {
                Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//截取整个屏幕
                Graphics g = Graphics.FromImage(myImage);
                g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
                g.ReleaseHdc(g.GetHdc());
                return myImage;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            getScreen();
        }
    }
}

LockBitmap类 是我网上找的资料,速度一般

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace WindowsFormsApp4
{
    public class LockBitmap
    {
        Bitmap source = null;
        IntPtr Iptr = IntPtr.Zero;
        BitmapData bitmapData = null;

        public byte[] Pixels { get; set; }
        public int Depth { get; private set; }
        public int Width { get; private set; }
        public int Height { get; private set; }

        public LockBitmap(Bitmap source)
        {
            this.source = source;
        }

        /// 
        /// Lock bitmap data
        /// 
        public void LockBits()//传入bmp,
        {
            try
            {
                // Get width and height of bitmap
                Width = source.Width;
                Height = source.Height;

                // get total locked pixels count
                int PixelCount = Width * Height;//像素

                // Create rectangle to lock
                Rectangle rect = new Rectangle(0, 0, Width, Height);//指定要锁定的部分

                // get source bitmap pixel format size检验是否是8.24.32位图像
                Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

                // Check if bpp (Bits Per Pixel) is 8, 24, or 32
                if (Depth != 8 && Depth != 24 && Depth != 32)
                {
                    throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
                }

                // Lock bitmap and return bitmap data
                bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                source.PixelFormat);

                // create byte array to copy pixel values
                int step = Depth / 8;
                Pixels = new byte[PixelCount * step];//如果是24位,像素*3=所有RGB数组组。
                Iptr = bitmapData.Scan0;//指针放到第一个数据。也就是第一个像素的B

                // Copy data from pointer to array
                Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);//复制到Pixel,传入bmp的数据复制到Pixel数组
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// Unlock bitmap data
        /// 
        public void UnlockBits()
        {
            try
            {
                // Copy data from byte array to pointer
                Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);

                // Unlock bitmap data
                source.UnlockBits(bitmapData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// Get the color of the specified pixel
        /// 
        /// 
        /// 
        /// 
        public Color LockGetPixel(int x, int y)
        {
            Color clr = Color.Empty;

            // Get color components count
            int cCount = Depth / 8;//Depth表示多少位数据

            // Get start index of the specified pixel//获得指定像素的起始索引
            int i = ((y * Width) + x) * cCount;

            if (i > Pixels.Length - cCount)
                throw new IndexOutOfRangeException();//抛出新异常

            if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                byte a = Pixels[i + 3]; // a
                clr = Color.FromArgb(a, r, g, b);
            }
            if (Depth == 24) // For 24 bpp get Red, Green and Blue
            {
                byte b = Pixels[i];
                byte g = Pixels[i + 1];
                byte r = Pixels[i + 2];
                clr = Color.FromArgb(r, g, b);
            }
            if (Depth == 8)
            // For 8 bpp get color value (Red, Green and Blue values are the same)
            {
                byte c = Pixels[i];
                clr = Color.FromArgb(c, c, c);
            }
            return clr;
        }

        /// 
        /// Set the color of the specified pixel
        /// 
        /// 
        /// 
        /// 
        public void LockSetPixel(int x, int y, Color color)
        {
            // Get color components count
            int cCount = Depth / 8;

            // Get start index of the specified pixel
            int i = ((y * Width) + x) * cCount;

            if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
                Pixels[i + 3] = color.A;
            }
            if (Depth == 24) // For 24 bpp set Red, Green and Blue
            {
                Pixels[i] = color.B;
                Pixels[i + 1] = color.G;
                Pixels[i + 2] = color.R;
            }
            if (Depth == 8)
            // For 8 bpp set color value (Red, Green and Blue values are the same)
            {
                Pixels[i] = color.B;
            }
        }
    }
}

 

你可能感兴趣的:(C# 两张图片像素比较,取出差异像素,效率甩图片压缩N+1倍)