question in WPF Image

Page 1 of 1

8 Replies - 9253 Views - Last Post: 21 July 2011 - 04:26 PM Rate Topic: -----

#1 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

question in WPF Image

Posted 21 July 2011 - 10:21 AM

Hi guys

1- how can i set the image source through my code not through XAML code ??
2- i look at the Image events but i didn't found mouseover event so what i have to do ??
i want to make the image opacity increasing when the mouse is over it ..

i am make a button (name: button1) that when i click on it the Image opacity (image name : image1) increasing .
This is the button code :
for (double i = 0; i <= 1; i = i + 0.1)
            {
                image1.Opacity = i;
                Thread.Sleep(50);
            }


but when i click at the button the image opacity is increasing directly not like gradient
i try t increase the sleep time but it still the same problem .

thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: question in WPF Image

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6519
  • View blog
  • Posts: 14,408
  • Joined: 02-June 10

Re: question in WPF Image

Posted 21 July 2011 - 10:31 AM

Thread.sleep doesn't do what you think it does.
It does not allow the form to update itself to the new opacity. It really does SLEEP EVERYTHING. Nothing happens, including updating the form.

You need to use a better timer using DateTime for example to give yourself a pause and let the form do its own behind-the-scenes work.

For example:

// Do something
DateTime timeOut = DateTime.Now.AddMilliseconds(50);
While (DateTime.Now < timeOut) { Application.DoEvents(); }
// Continue after 50 ms pause

Was This Post Helpful? 0
  • +
  • -

#3 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: question in WPF Image

Posted 21 July 2011 - 11:24 AM

private void button1_Click(object sender, RoutedEventArgs e)
        {
            DateTime timeout = DateTime.Now.AddMilliseconds(50);
            double i = 0;
            while (DateTime.Now < timeout)
            {
                image1.Opacity = i;
                i = i + 0.1;
            }
            MessageBox.Show(i.ToString());
        }


after the 50 second => i=2417.7xxxx
i mean the while loop is very quickly so i can't see the image opacity changing
Was This Post Helpful? 0
  • +
  • -

#4 bflosabre91  Icon User is offline

  • D.I.C Lover

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: question in WPF Image

Posted 21 July 2011 - 12:16 PM

i think what your trying to do can be accomplished with something like this

i.BeginAnimation((Image.OpacityProperty), New DoubleAnimation(0.5, 1, New Duration(New TimeSpan(0, 0, 1))))



"i" would be your image control
Was This Post Helpful? 1
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: question in WPF Image

Posted 21 July 2011 - 12:21 PM

I agree with bflosabre91. Don't use a timer to simulate an animation in WPF. Do an animation. Timers to simulate animations is for WinForms.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6519
  • View blog
  • Posts: 14,408
  • Joined: 02-June 10

Re: question in WPF Image

Posted 21 July 2011 - 12:29 PM

You're not getting what I am saying.

You need to do NOTHING during the pause between levels of opacity.

What you have done here is just make a really fast loop, not really any different than what you had before. Of course you won't see the change.



private void button1_Click(object sender, RoutedEventArgs e)
        {
            DateTime timeout = DateTime.Now.AddMilliseconds(50);
            for (double i = 0; i <= 1; i = i + 0.1)
            {
               image1.Opacity = i;  // Set the opacity
               while (DateTime.Now < timeout)  // Wait a little bit
               {
                   Application.DoEvents();//To update the GUI
               }
               i += .01; // Increase the opacity for the next iteration through the 'while' loop.
            }
            MessageBox.Show(i.ToString());
        }



View Posteclipsed4utoo, on 21 July 2011 - 01:21 PM, said:

I agree with bflosabre91. Don't use a timer to simulate an animation in WPF. Do an animation. Timers to simulate animations is for WinForms.


Agree with this as well. I just didn't want to take the OP away from their original question since they were obviously having trouble with the basic concept and couldn't follow the program flow to make what they started with work.

Sure there are lots of ways to improve any chuck of code. But I wanted to at least help the OP understand this issue and not just abandon it for something else, before moving forward.

I would also suggest items 2 and 5 below to help clean up your code a bit.
Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

  • Learn how to search the 'net for examples. How to do a good search that will get you targeted answers.

  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    FormMain_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    

This post has been edited by tlhIn`toq: 21 July 2011 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: question in WPF Image

Posted 21 July 2011 - 02:31 PM

View Postbflosabre91, on 21 July 2011 - 12:16 PM, said:

i think what your trying to do can be accomplished with something like this

i.BeginAnimation((Image.OpacityProperty), New DoubleAnimation(0.5, 1, New Duration(New TimeSpan(0, 0, 1))))



"i" would be your image control


thanks i dont know this function before cause if beginner in WPF
but it's work great .

thanks tlhIn`toq
but the line :

Application.DoEvents();//To update the GUI

make an error in compiling

But what about mouseover event how can i set The BeginAnimation function when the mouse is over the image ??
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: question in WPF Image

Posted 21 July 2011 - 03:54 PM

There is no Application.DoEvents() in WPF. That's why you are getting a compile error.

There is an event for the MouseEnter event for the Image control in WPF.
Was This Post Helpful? 0
  • +
  • -

#9 morsev  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 70
  • Joined: 26-September 10

Re: question in WPF Image

Posted 21 July 2011 - 04:26 PM

Thank you very much eclipsed4utoo

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1