0

How can I check if two images have same source in wp7 C#? I am doing this but it's not working.

if(image1.source.tostring()==image2.source.tostring()){}
Niall C.
  • 10,878
  • 7
  • 69
  • 61
Ahad Siddiqui
  • 331
  • 1
  • 2
  • 13

3 Answers3

2

I can't see any easy way to do that. ImageSource is an abstract class and can be anything therefore it is hard to compare. However the most likely implementation is BitmapImage. So you can check of its type and if it is BitmapImage or IUriContext you can cast it and compare the BaseUri property.

Andras Csehi
  • 4,305
  • 1
  • 28
  • 36
0

You're comparing references, to compare values...

if(image1.source.ToString().Equals(image2.source.ToString())) {}
Konstantin
  • 3,254
  • 15
  • 20
  • thanks but when the source are different , this code make the condition true ! – Ahad Siddiqui Dec 18 '13 at 18:30
  • if you want to inverse put `not` symbol in front of condition (`!`) - if(!image1...) – Konstantin Dec 18 '13 at 18:39
  • 1
    Are you both joking? ToString() method returns string type, and (string) == (string) must have [same result](http://stackoverflow.com/questions/1659097/c-string-equals-vs) as (string).Equals(string) – Pavel K Dec 18 '13 at 18:41
  • thats why it always make the condition true . Thanks pavel but how can i do this ?? – Ahad Siddiqui Dec 18 '13 at 18:45
  • There is more details needed. It will be great to see your XAML with more codebehind (c#). – Pavel K Dec 18 '13 at 18:48
  • thanks pavel , but i found the solution ........ BitmapImage bm1 = (BitmapImage)image1.Source; BitmapImage bm2 = (BitmapImage)image2.Source; bool same =(string.Compare(bm1.UriSource.AbsoluteUri,bm2.UriSource.AbsoluteUri) == 0); share|improve this answer – Ahad Siddiqui Dec 18 '13 at 19:00
  • Just saw you comment. Basically it's what I said but keep in mind that source is not necessary Bitmapimage. – Andras Csehi Dec 18 '13 at 23:30
0

You can compare both of the images with the help of BitmapImage

BitmapImage image1= new BitmapImage(new Uri("your first image relative path", UriKind.Relative))

BitmapImage image2= new BitmapImage(new Uri("your second image relative path", UriKind.Relative))

if(image1==image2)
{
MessageBox.show("Image matched.");
}
else
{
MessageBox.Show("Not matched.");
}
Jaihind
  • 2,770
  • 1
  • 12
  • 19