WPF Image background sprites image - wpf

In WPF image control,
< Image Source="icon.png" />
Now I want to change icon, without using the other image as icon1.png,
Is there a way to set background position like in css?
So that i can keep multiple icon images in icon.png and show the one required rather then having multiple images as icon1.png, icon2.png.
Any help?
Thanks

<Image>
<Image.Source>
<CroppedBitmap Source="<some path>" SourceRect="X,Y,W,H" />
</Image.Source>
</Image>
Just a small example. For further details you can see MSDN.

Related

How to create border for png image

I have a custom image in .png format. I want to create button. The form of button is this image. So I put the image inside button template and want it be fleshed only when mouse over the image. The shape of image is rectangle (http://prntscr.com/lz5m5q), but in fact it is custom shape. So how can I set border for button and ignore transparent background of image (I don't want see border as rectangle, border should be around my actual image).
hope i got correctly, is this you want ?
Style Link
due to characters limit i am posting style in above link. copy paste codes from above link in Windows.Resources or where you want.
<Window.Resources>
<!-- I put this in windows resources you can put it app.xaml or any resource dictionary -->
<!-- Copy here content from the above link link because of stackoverflow character limit i cant paste it here. -->
</Window.Resources>
<Grid>
<Button Height="250" Width="300" Style="{DynamicResource CountryButtonStyle}" />
</Grid>
Output:

Saving source string of image in richtextbox after moving or pasting from clipboard

I'm working on richtextbox, which can handle images. I'm saving xaml content of richtextbox to database as string. Images are saved in the folder tree. And I have one problem:
When I insert image to richtextbox (in InlineUIContainer) all is working, saving and loading makes no problem. But when I drag image or cut and paste image again, source path of image is changed:
<Image>
<Image.Source>
<BitmapImage BaseUri="pack://payload:,,wpf1,/Xaml/Document.xaml"
UriSource="./Image1.jpeg" CacheOption="OnLoad" />
</Image.Source>
</Image>
original source was:
<Image Source="pack://siteoforigin:,,,/path_to_image/some_image.jpg" />
And therefore, when I save xaml again (still everything ok), I can't load images again, because in the xaml there are wrong paths to them (not ok :-)).
I searched for it, but I haven't found any solution. Could You help me please?
I found another way, I am saving it to the XAML package, Images are copied into it and everything works perfect.

Scaling an image with WP7 Silverlight with no smoothing algorithm applied

I have some images (32x32 .png) that I want to display in my Windows Phone 7 application. Right, now, I am able to scale them, etc... I was wondering if there was a way to scale them without any smoothing algorithms applied (so when I double the image size, it creates a blocky image look).
Right now in my XAML I have the following:
<Image Height="64" Width="64" Margin="12,0,9,0" Name="{Binding itemName}"
Stretch="Uniform" VerticalAlignment="Center" Source="{Binding imageName}" />
Where imageName is just a path to the .png images. Is there a simple way to do this in just XAML, or should I be loading the image into a different format to play with it in the code (while keeping the transparency of the png).
Thanks in advance,
-Jeff
Theres currently no way to do this in XAML. The only way to achieve a nearest neighbor scaling without any interpolation (smoothing) is the open source library WriteableBitmapEx.
http://writeablebitmapex.codeplex.com
First load the PNG into a WriteableBitmap, then use the Resize extension method the WriteableBitmapEx provides. Use the NearestNeighbor as value for the last parameter of the method. Then assign the return value of the Resize method to the Source property of your Image control.
You can wrap this functionality in an IValueConverter implementation so you can use it in XAML.

How can I change the color of an image in silverlight/WP7 (Color Mask)?

I have a toggle button with a png that has a transparent background and a black foreground. If the button is selected then I want the black color of the image to change to a color chosen by the user. Is there a way to do this in Silverlight and/or wp7?
So for example:
<ToggleButton>
<Image Source="MyImage.png" />
</ToggleButton>
MyImage.png has a transparent background and a black foreground. The user's preferred color is red. When the button is toggled on I want the black foreground of the image to turn red.
I would try OpacityMask approach. Basicaly it should look something like this:
<Rectangle Fill="Red">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="MyImage.png"/>
</Rectangle.OpacityMask>
</Rectangle>
by changing rectangle's fill property you would get different colored image.
How much control does the user have over the colour?
If they are picking from a limited set (e.g. red, green, blue, black, brown) then the simplest thing to do would be to bind the image source to a variable which holds the name of the image and then change which name is held in that variable.
If they can pick any colour then you need to do some image processing to change the black pixels of a reference image to the selected colour, write that to isolated storage and then bind the image source to that new file.
Another alternative is to draw the button in XAML and then you can have direct control over the foreground colour. This MSDN page describes the basics of drawing. You can use the same commands to define the image on a button, as described on this page from Scott Gu's blog:
(this is the image of the code from the blog).
If you bind the colour to a variable then the user can change the colour of the image. It does rely on you being able to draw the image in XAML though.

In WPF, What is the best way to create toolbar buttons so that the images are properly scaled?

Specifically, I'm looking to use the 16*16 32-bit png images included with the VS2008ImageLibrary. I've tried manually setting the Height and Width attributes of the image, adjusting margins and padding, adjusting Stretch and RenderOptions. My attempts to create toolbar buttons have all led to either Improper Scaling (blurry icons), the bottom row of pixels on the icon being truncated, or the toolbar button being improperly sized - not to mention the disappearing icons already mentioned Here. Has anyone found the best way to make standard, VisualStudio/WinForms-style toolbar buttons that display properly in WPF?
First, change the image resolution to 96DPI, this can done with the free Paint.net ( http://www.getpaint.net ) by opening the file, Selecting Image->Canvas Size from the menu and adjusting the "resolution" to 96 and saving.
If this doesn't help you can then use the solution I wrote about in my blog here http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx
Best way would be using Vector graphics instead of png. I know the following is not exactly what you asked for, but imho there is no way for better looking icons. Also it would help you get rid off margins and paddings. (Ok, if you want to use photos you're screwed)
Bad News is you probably need to repaint all your icons. You could do this using MS Expression Blend(it's capable to save painted Images as .xaml) or you make them on our own with a texteditor.
I prefer the Border.Background instead of the Image.Source for placing the icon, this allows me to put text over the image. This would look samething like that:
<Window.Resources>
<ResourceDictionary Source="Resources/Icons.xaml"/>
</Window.Resources>
<!--
...
-->
<Button>
<Border Background="{StaticResource IconName}" Height="16" Width="16" />
</Button>
The best workaround I can come up with is this:
<Image x:Key="TB_NewIcon" Source="Toolbar Images/NewDocumentHS.png" Height="16" Width="16" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
...
<Button Command="ApplicationCommands.New" Content="{StaticResource TB_NewIcon}" Padding="2,2,2,1"/>
Or Alternatively:
<BitmapImage x:Key="TB_NewIcon" UriSource="Toolbar Images\NewDocumentHS.png"/>
...
<Button Command="ApplicationCommands.New" Padding="2,2,2,1">
<Image Source="{StaticResource TB_NewIcon}" Height="16" Width="16" SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
</Button>
For the Button Tag, the Padding attribute is needed to ensure that the image isn't truncated at a height of 15 pixels, and that the button isn't resized to fit the image. Alternatively, we could specify Padding="1", but then we must manually set Height="21" and Width="22" to ensure the button isn't resized to fit the image
On the Image Tab, the Height and Width are needed to ensure that the image isn't stretched. SnapsToDevicePixels and RenderOptions.BitMapScalingMode are both needed to ensure that there is no blurring. I can't promise that this will work nicely for all resolutions.
Note:
For the NewDocumentHS.png icon, the one that causes the most issues,as it takes up the full 16 pixels of height, you may want to adjust the padding to "1,1,3,2", so that the bottom aligns more properly with other icons.
You may want to consider trying a new property available now in WPF4.
Leave the RenderOptions.BitmapScalingMode to HighQuality or just don't declare it.
On your root element (i.e. your main window) add this property: UseLayoutRounding="True".
A property previously only available in Silverlight has now fixed all Bitmap sizing woes. :)
Please Note - a few of the effects layout rounding
can have on exact layout:
width and or height of elements may grow or shrink by at most 1 pixel
placement of an object can move by at most 1 pixel
centered elements can be vertically or horizontally off center by at most
1 pixel
More info found here: http://blogs.msdn.com/text/archive/2009/08/27/layout-rounding.aspx

Resources