Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
1.75/5 (3 votes)
See more:
i want if that image control will contain gol1s2.gif image it will transfer to window2.xaml form otherwise it will transfer to window3.xaml form.
<window x:class="WpfApplication1.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="509">
    <grid height="275" width="271">
        <Image Margin="10,10,61,115" Grid.Row="0" Grid.Column="0"  Source="C:\Users\New1\Desktop\ba\WpfApplication1\WpfApplication1\img\gol1s2.gif" Name="image1" Stretch="Fill" />
        <Button Height="23" Margin="100,0,96,48" Name="btnSelect" VerticalAlignment="Bottom" Click="btnSelect_Click">Select</Button>
    </grid>
</window>

page behind code is
string a=@"C:\Users\New1\Desktop\ba\WpfApplication1\WpfApplication1\img\gol1s2.gif";
            if(image1=a)
            {
                Window2 r=new Window2();
                r.Show();
            }
            else
            {
                 Window3 rr=new Window3();
                rr.Show();
            }


In this coding i am getting error is


Cannot implicitly convert type 'string' to 'System.Windows.Controls.Image'


Sir i am getting that error only
Posted
Updated 28-Mar-11 2:15am
v3
Comments
Slacker007 29-Mar-11 6:02am    
@apfelmuuus: Why don't you take this comment and add it as a solution...maybe expand your solution a little. Then delete this comment. Don't be afraid to answer a question if you think you have a possible solution. :)
Apfelmuuus 29-Mar-11 7:23am    
Done!
Thank you for giving the necessary motivation:)

There is a protected property of Image called BaseUri so you could do a little hack and create a new class that inherits the Image class and has got a function returning base.BaseUri.AbsolutePath. Then call this function to check wether the image refers to the path or not.

However, I think there could be a better solution I don't know yet.

[Edit]

Now I've got one:)

I'm using the Image.Source property that refers to a URI so I have to declare the string to check equality as follows:
string uri = "C:/Users/New1/Desktop/ba/WpfApplication1/WpfApplication1/img/gol1s2.gif";

This is necessary because after instantiating the Image object (you can still use an absolute or relative path on your hard-disk-drive for instantiating) the reference to the Image-File is saved as URI.

Finally check if the correct image has been loaded:

if (image1.Source.ToString().Contains(uri))
{
    // Do something
}
 
Share this answer
 
v3
Comments
Slacker007 29-Mar-11 7:25am    
There you go! :)
Tarun.K.S 29-Mar-11 7:27am    
Nope I tried this before but it didn't work.
The problem is your uri has this "/" slash which is not correct. Its this "\" slash.
Apfelmuuus 29-Mar-11 7:45am    
Maybe it's a reason of the framework you are using. There are some differences between .net 3.0 , 3.5 and 4.0!

I tried both slashes and figured out that "\" doesn't work in my case but there are no problems with "/". Furthermore an URI always uses "/".

@Tarun.K.S. Which framework version do you use?
Tarun.K.S 29-Mar-11 7:48am    
I have Framework 3.5.
The point is that the path which Windows recognizes is with "\" slash.
Apfelmuuus 29-Mar-11 8:06am    
That's right!
However, Image.Source doesn't refer to a path on hdd but rather to an URI so I cannot understand why "\" has worked in your case. Did you use Image.Source or something else?
Apfelmuus was close, but the problem is in the URI.

Here is one way of solving it but it may not be an elegant one.
C#
String orgFile = @"C:\Documents and Settings\aUser\My Documents\My Pictures\Winter.jpg";
String orf = orgFile.Replace("\\", "/");
ImageSource imgSource = image1.Source;
String str = imgSource.ToString();
String fileName = str.Remove(0, 8);
if (fileName.Equals(orf))
{
    MessageBox.Show("Equal");
}


A few problems that I encountered was that :

1) the String str returns the source like this : "file:///C:\Documents and Settings\aUser\My Documents\My Pictures\Winter.jpg". Its of Pack URI form.
Solution : so I removed the first 8 letters.

2) next was that it won't work with Images stored as resources in your application as the source will return a pack uri like this : "pack://application:,,,/WpfApplication1;component/Winter.jpg".

If this solution doesn't suit you, then the first solution provided by Apfelmuus can be considered.
 
Share this answer
 
Comments
Apfelmuuus 29-Mar-11 8:14am    
:D that's nice!

String orf = orgFile.Replace("\\", "/"); means that you replace "\\" with "/" and that's exactly what I've said so we just misunderstood eachother!
Tarun.K.S 29-Mar-11 8:23am    
I had to Replace "\\" of the local path with "/" because the Source property of the Image returns the path with "/" slash. :)
Apfelmuuus 29-Mar-11 8:28am    
Exact...
I'm glad we found the problem!
Tarun.K.S 29-Mar-11 8:47am    
Yay!
Sergey Alexandrovich Kryukov 1-Apr-11 16:37pm    
Good catch, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900