views:

2223

answers:

5

I've got a very large image which I'd like to use for sprite techniques (à la css image sprites).

I've got the code below:

<Image x:Name="testImage" Width="24" Height="12" Source="../Resources/Images/sprites.png">
     <Image.Clip>
         <RectangleGeometry Rect="258,10632,24,12" />
     </Image.Clip>
</Image>

This clips the source image to 24x12 at the relative position of 258, 10632 in the source image.

The problem is that I want the cropped image to show at 0,0 in the testImage whereas it shows it at 258, 10632. It's using the geometry as a cutting guide but also as a layout guide.

Anyone have any idea how this should be done? if at all.

Conclusion: There seems to be no good way of doing this at present, Graeme's solution seems to be the closest to achieving this with Silverlight 2.0.

That said, if anyone knows of a better way of doing this, please reply with an answer.

+1  A: 

Assuming you're on a canvas

<Image x:Name="testImage" Width="24" Height="12" Canvas.Left="-258" Canvas.Top="-10632" Source="../Resources/Images/sprites.png">
 <Image.Clip>
     <RectangleGeometry Rect="258,10632,24,12" />
 </Image.Clip>

With WPF you would use a CroppedBitmap but unfortunately that doesn't exist in Silverlight.

< Edit >

With further experimentation a solution without using a canvas:

<Image x:Name="testImage" Width="24" Height="12" Source="../Resources/Images/sprites.png">
    <Image.Clip>
        <RectangleGeometry Rect="258,10632,24,12" />
    </Image.Clip>
    <Image.RenderTransform>
        <TranslateTransform X="-258" Y="10632"/>
    </Image.RenderTransform>
</Image>

It's doing the same thing as the canvas just slightly neater.

Graeme Bradbury
I like where you're going here but changing the image width to 24 hides the content. Changing the width at all changes which bit of the original image is being shown.
TreeUK
I could leave the width and height as the original Width="800" Height="18928" but is that the RIGHT way to be doing it?
TreeUK
The problem is that Silverlight doesn't yet support what you're trying to do. CroppedBitmap is the RIGHT way, but unfortunately only exists in WPF. So unless you break open Reflector and implement it yourself you're left with the limitation of Image
Graeme Bradbury
So, this is currently the only way of doing it at the moment.. Rubbish! I guess I'll have to look to the Silverlight 3 managed bitmap API for hope.
TreeUK
+1  A: 

The Bitmap API of Silverlight 3.0 will allow you to grap a clip from your sprite image.

See this post on How to crop instead of clip in silverlight

TJB
A: 

Why do this at all, the whole point of css image sprites is to improve download time by making one request instead of many. But you can achieve the same by just putting all your images in a xap (or THE xap) and download them in one request.

The issue I perceive is more the loading of a multitude of images rather than using a single image that *hopefully* Silverlight will internally optimise the performance of.
TreeUK
I would bet that you're actually going to get higher memory usage and worse performance doing that. Unless you actually have a proven bottleneck AND measurements showing this "optimization" can improve things (which sounds like no on both counts) then you are wasting your time AND decreasing the quality of whatever it is you're working on.
A: 

It turns out this can be done.

<Rectangle x:Name="myRect" Width="28" Height="12" />

ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = //Load in image source
imageBrush.Stretch = Stretch.None;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.AlignmentY = AlignmentY.Top;

TranslateTransform offsetTransform = new TranslateTransform();
offsetTransform.X = -258;
offsetTransform.Y = -10632;

imageBrush.Transform = offsetTransform;
TreeUK
A: 

This is perfectly doable -- the solution is to use negative margins rather than transforms. Just set a negative margin top and left to eat up the offset of the clip top/left (258,10632 in your example). Negative margins are also needed right and bottom to eat up the remainder of the source image; the formula for the right margin is:

-1 * (width_of_source - x_coord_of_clip - width_of_clip_region)

Nestor