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");

        }

        /// <summary>
        /// 截全屏並保存成Bitmap
        /// </summary>
        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;
        }

        /// <summary>
        /// Lock bitmap data
        /// </summary>
        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;
            }
        }

        /// <summary>
        /// Unlock bitmap data
        /// </summary>
        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;
            }
        }

        /// <summary>
        /// Get the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Set the color of the specified pixel
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="color"></param>
        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;
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM