Cropping images in WPF

Page 1 of 1

3 Replies - 8830 Views - Last Post: 02 March 2013 - 06:39 PM Rate Topic: -----

#1 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Cropping images in WPF

Posted 02 March 2013 - 02:56 PM

Hi,

I have 2 images. The first are 5 golden stars, the 2nd are 5 silver stars. I want to make a rating usercontrol where I have to add a score and the usercontrol does the rest. The 2nd part I can do on my own, it just the first part with the images.
    <Grid>
        <Viewbox HorizontalAlignment="Left">
            <Image Stretch="None">
                <Image.Source>
                    <CroppedBitmap Source="/Movie List WPF;component/Resources/5StarSilver.png" 
                                   SourceRect="30 0 75 50"/>
                </Image.Source>
            </Image>
        </Viewbox>
    </Grid>
So, I'm not getting any image in my Viewbox. If I do it with only an Image and same source it works fine but I'm not able to crop it then. Am I doing something wrong in my code with the CroppedBitmap? I'm really clueless on this one.

Is This A Good Question/Topic? 0
  • +

Replies To: Cropping images in WPF

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Cropping images in WPF

Posted 02 March 2013 - 03:23 PM

Personally I'd make a UserControl.
Give it a property of 'NumberOfStars' so it can dynamically adjust itself to need. Because as soon as you get it in the world your boss is going to say "Change it to 10 instead of 5".

Give it an event it can raise when the user clicks on one of the individual star images like SelectionMade with an argument of the value based on which star was clicked.

Give it a property of "CurrentValue" so you can bind to it.

Now you can add as many as you like on your window, set the number of stars property to 5 and bind the CurrentValue of the control to a property in your window.

So I guess I"m saying, I wouldn't use a single image of 5 stars because its a pain in the arse and not dynamic for any use other than 5.

By doing the binding (two way) you can set a value in your Window property and have the UserControl update to show 3 or 5 or whatever. You want that two way relationship.

You might consider not using images. A star is easy to paint. That way you can make one control that actually draws x number of stars in a supplied style or color. Now you have red/green or black/white or Gold/Silver. Plus you get that DirectDraw advantage of scalability without the bitmaps getting pixelated.

This post has been edited by tlhIn`toq: 02 March 2013 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Cropping images in WPF

Posted 02 March 2013 - 03:48 PM

So what about 2.5/5. How can I get half stars done then?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5442
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Cropping images in WPF

Posted 02 March 2013 - 06:39 PM

Again, this is personal opinion, but I've always hated that shit.
2.5/5 *is* 5 out of 10.

If your user is on a mobile device its VERY hard to hit half a star with precision.

But you could make a behavior for a star so it is like a 3-date check.
Default is off
Click once for full - the normal default behavior.
Click again for 1/2

When someone clicks a different star you you reset higher stars to default (off) and lower stars to full.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1