日期:2014-05-20  浏览次数:20983 次

Image 读取
怎么直接从数据库中判断Image类型字段的记录是否相等

------解决方案--------------------
用二进制比较试试。
------解决方案--------------------
image无法直接比较,他要比较的话,得逐字节比较

实际上你可以提取image的MD5值作为特征值,然后比较其特征值是否相等
------解决方案--------------------
逐字节比较的例子
C# code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        byte[] temp1 = getImage("d://1.jpg");
        byte[] temp2 = getImage("d://2.jpg");
        bool isEqual=true;
        //判断图片是否相等
        if (temp1.Length==temp2.Length)
        {
            for (int i = 0; i < temp1.Length; i++)
            {
                if (temp1[i] != temp2[i])
                {
                    isEqual = false;
                }
            }
        }
        else
        {
            isEqual = false;
        }
        if (isEqual)
        {
            Response.Write("<script>alert('两图片相等')</script>");
        }
        else
        {
            Response.Write("<script>alert('两图片不相等')</script>");
        }
    }
    /// <summary>
    /// 按图片路径把图片打成二进制流
    /// </summary>
    /// <param name="path">图片路径</param>
    /// <returns></returns>
    public byte[] getImage(string path)
    {
        FileStream fs = File.OpenRead(path);
        MemoryStream ms = new MemoryStream();
        int bdata;
        while ((bdata = fs.ReadByte()) != -1)
        {
            ms.WriteByte(((byte)bdata));
        }
        byte[] data = (byte[])ms.ToArray();
        fs.Close();
        ms.Close();
        fs = null;
        ms = null;
        return data;
    }
}