C#如何比较两个pictureBox是不是使用了同一张图片?
if(pictureBox1.Image==pictureBox2.Image&&pictureBox2.Image==pictureBox3.Image){}这样不行啊...
if (pictureBox1.Image == pictureBox2.Image && pictureBox2.Image == pictureBox3.Image)
{
}
这样不行啊! 展开
{
}
这样不行啊! 展开
6个回答
展开全部
1、获取图片的 byte[] 然后利用MD5 码比较
2、获取图片的 byte[] 然后合并成字符串进行比较
void Form1_Load(object sender, EventArgs e)
{
var pic1 = new PictureBox();
var pic2 = new PictureBox();
var pic3 = new PictureBox();
//pic1.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img25.jpg");
//pic2.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img26.jpg");
//pic3.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img26.jpg");
pic1.Image = SystemIcons.Application.ToBitmap();
pic2.Image = SystemIcons.Warning.ToBitmap();
pic3.Image = SystemIcons.Warning.ToBitmap();
var ms1 = new System.IO.MemoryStream();
var ms2 = new System.IO.MemoryStream();
var ms3 = new System.IO.MemoryStream();
pic1.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp);
pic2.Image.Save(ms2, System.Drawing.Imaging.ImageFormat.Bmp);
pic3.Image.Save(ms3, System.Drawing.Imaging.ImageFormat.Bmp);
var buffer1 = ms1.GetBuffer();
var buffer2 = ms2.GetBuffer();
var buffer3 = ms3.GetBuffer();
var md5 = new System.Security.Cryptography.HMACMD5();
var md5_ms1 = md5.ComputeHash(buffer1);
var md5_ms2 = md5.ComputeHash(buffer2);
var md5_ms3 = md5.ComputeHash(buffer3);
var sb = new StringBuilder();
Console.WriteLine("buffer1: {0}", string.Join(",", buffer1));
Console.WriteLine("buffer2: {0}", string.Join(",", buffer2));
Console.WriteLine("buffer3: {0}", string.Join(",", buffer3));
Console.WriteLine();
Console.WriteLine("md5_ms1: {0}", string.Join(",", md5_ms1));
Console.WriteLine("md5_ms2: {0}", string.Join(",", md5_ms2));
Console.WriteLine("md5_ms3: {0}", string.Join(",", md5_ms3));
Console.WriteLine();
Console.WriteLine("byte:");
Console.WriteLine("buffer1 == buffer2: {0}", buffer1 == buffer2);
Console.WriteLine("buffer2 == buffer3: {0}", buffer2 == buffer3);
Console.WriteLine("md5_ms1 == md5_ms2: {0}", md5_ms1 == md5_ms2);
Console.WriteLine("md5_ms2 == md5_ms3: {0}", md5_ms2 == md5_ms3);
Console.WriteLine();
Console.WriteLine("string:");
Console.WriteLine("buffer1 == buffer2: {0}", string.Join(",", buffer1) == string.Join(",", buffer2));
Console.WriteLine("buffer2 == buffer3: {0}", string.Join(",", buffer2) == string.Join(",", buffer3));
Console.WriteLine("md5_ms1 == md5_ms2: {0}", string.Join(",", md5_ms1) == string.Join(",", md5_ms2));
Console.WriteLine("md5_ms2 == md5_ms3: {0}", string.Join(",", md5_ms2) == string.Join(",", md5_ms3));
Console.WriteLine();
}
2、获取图片的 byte[] 然后合并成字符串进行比较
void Form1_Load(object sender, EventArgs e)
{
var pic1 = new PictureBox();
var pic2 = new PictureBox();
var pic3 = new PictureBox();
//pic1.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img25.jpg");
//pic2.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img26.jpg");
//pic3.Image = Image.FromFile(@"C:\Windows\Web\Wallpaper\Scenes\img26.jpg");
pic1.Image = SystemIcons.Application.ToBitmap();
pic2.Image = SystemIcons.Warning.ToBitmap();
pic3.Image = SystemIcons.Warning.ToBitmap();
var ms1 = new System.IO.MemoryStream();
var ms2 = new System.IO.MemoryStream();
var ms3 = new System.IO.MemoryStream();
pic1.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp);
pic2.Image.Save(ms2, System.Drawing.Imaging.ImageFormat.Bmp);
pic3.Image.Save(ms3, System.Drawing.Imaging.ImageFormat.Bmp);
var buffer1 = ms1.GetBuffer();
var buffer2 = ms2.GetBuffer();
var buffer3 = ms3.GetBuffer();
var md5 = new System.Security.Cryptography.HMACMD5();
var md5_ms1 = md5.ComputeHash(buffer1);
var md5_ms2 = md5.ComputeHash(buffer2);
var md5_ms3 = md5.ComputeHash(buffer3);
var sb = new StringBuilder();
Console.WriteLine("buffer1: {0}", string.Join(",", buffer1));
Console.WriteLine("buffer2: {0}", string.Join(",", buffer2));
Console.WriteLine("buffer3: {0}", string.Join(",", buffer3));
Console.WriteLine();
Console.WriteLine("md5_ms1: {0}", string.Join(",", md5_ms1));
Console.WriteLine("md5_ms2: {0}", string.Join(",", md5_ms2));
Console.WriteLine("md5_ms3: {0}", string.Join(",", md5_ms3));
Console.WriteLine();
Console.WriteLine("byte:");
Console.WriteLine("buffer1 == buffer2: {0}", buffer1 == buffer2);
Console.WriteLine("buffer2 == buffer3: {0}", buffer2 == buffer3);
Console.WriteLine("md5_ms1 == md5_ms2: {0}", md5_ms1 == md5_ms2);
Console.WriteLine("md5_ms2 == md5_ms3: {0}", md5_ms2 == md5_ms3);
Console.WriteLine();
Console.WriteLine("string:");
Console.WriteLine("buffer1 == buffer2: {0}", string.Join(",", buffer1) == string.Join(",", buffer2));
Console.WriteLine("buffer2 == buffer3: {0}", string.Join(",", buffer2) == string.Join(",", buffer3));
Console.WriteLine("md5_ms1 == md5_ms2: {0}", string.Join(",", md5_ms1) == string.Join(",", md5_ms2));
Console.WriteLine("md5_ms2 == md5_ms3: {0}", string.Join(",", md5_ms2) == string.Join(",", md5_ms3));
Console.WriteLine();
}
展开全部
xiehuanxie 说的是可能是唯一可以解决的办法。但是这样很慢。也可以取图像流的哈希值或是md5值。
不过你的问题可以这样解决,在给picturebox 赋值后将原始的image对象或是文件名同时赋值给Tag属性,这样比较两个picturebox中的图像是否相同时只要比较tag属性主可以了,这样也容易很多。
不过你的问题可以这样解决,在给picturebox 赋值后将原始的image对象或是文件名同时赋值给Tag属性,这样比较两个picturebox中的图像是否相同时只要比较tag属性主可以了,这样也容易很多。
更多追问追答
追问
可否给几句简短的代码参考下?
追答
你的 pictureBox.Image 是哪里来的,从文件中载入的还是通过资源文件赋值的?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if (pictureBox1.Image.Tag == pictureBox2.Image.Tag)
{
MessageBox.Show("是同一张");
}
两个图片的相关数据完全相同 所以是同一张
给大拇指哇!!!
{
MessageBox.Show("是同一张");
}
两个图片的相关数据完全相同 所以是同一张
给大拇指哇!!!
追问
试了,不同的图片也会判断成相同的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接比对地址不可以么,同一目录下只允许同名文件存在一个。
比对引用地址是否相同。
如果是不同文件夹下的话,xiehuanxie的方法应该可以,不过我没试过。
比对引用地址是否相同。
如果是不同文件夹下的话,xiehuanxie的方法应该可以,不过我没试过。
更多追问追答
追问
如何对比地址?有没有简短易懂的代码?
追答
string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;获取程序的基目录,
或者System.Windows.Forms.Application.StartupPath获取程序启动目录。
Path.GetFullPath(你程序中文件的相对路径)——相对路径转换绝对路径
如果你的图片都是来自你项目中的资源,以上方法应该可以,如果是比对外部引用的来自不同路径的图片,或许只能用楼上的方法了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先检查Size,
然后用GetPixel方法对比每个像素
然后用GetPixel方法对比每个像素
追问
可否给两行代码?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询