How to create border for png image - wpf

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:

Related

Hide overflowing portions of textblock and image outside a wpf Button

I have a WPF button.The ContentControl inside this Button contains other UI elements like Textblock,Images etc.When i increase the font size of the Textblock or the Image etc,or i change the text inside Textblock,then some portion of the text and image can sometimes be seen overflowing outside the button's(ie parent control) height and width if the text ,fontsize or image size is more than the height and width of this button.I want to hide these overflowing portions of the text and image controls,ie which only the portions of the Textblock and Image which lie outside the button contentcontrol's height and width boundary.How can i do this?
I was able to do it.
I simply placed a the entire ContentControl inside a Label and it was done.
ie earlier it was like this:-
<Button>
<ContentControl>
<TextBlock/>
</ContentControl>
</Button>
Now i made it like this:-
<Button>
<Label>
<ContentControl>
<TextBlock1/>
<TextBock2/>
.
.
.
<Image1/>
<Image2/>
</ContentControl>
</Label>
</Button>

How to change the scaling algorithm that is applied to the icon in the window?

If large images are displayed in a size smaller than the original, then begin to show artifacts "pixelation":
https://habrastorage.org/files/8a6/198/506/8a61985069cb4d0097540bf0d07f4a68.png
But this can be solved by specifying the attribute of the object in XAML, scaling algorithm with higher quality:
<Image Height="50" Width="50"
Source="Resources/logo.png"
RenderOptions.BitmapScalingMode="HighQuality" /><!-- Scaling Mode -->
Then, the image looks fine:
https://habrastorage.org/files/819/863/48f/81986348f9ab454a9d6b676d88321d0a.png
Question. How to change the scaling algorithm that is applied to the icon in the window? Icon set through XAML (the last line):
<Window x:Class="MyApp.AboutWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:res="clr-namespace:MyApp.Properties"
Title="{x:Static res:Resources.AboutWindowTitle}"
Height="450" Width="300"
ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Icon="Resources/info.png"><!-- Icon -->
Icon in the title bar looks like this:
_https://habrastorage.org/files/946/bb7/67b/946bb767b66042aba985a8e55a21b078.png (Sorry, I do not have enough reputation to post third link)
Attribute RenderOptions.BitmapScalingMode = "HighQuality" applied to the window, gives nothing.
Unlike the elements in client area, those in title bar are not under direct control of a WPF application. A png file set to a Window's Icon property will be converted to the icon image in title bar (and that in task bar) but as you see, the conversion seems to be quite limited.
So as far as I know, unfortunately, the only practical solution to set nicely shown icon in title bar is to create or find an icon file (.ico) which has multiple images inside and use it instead of merely a png file.

WPF Image background sprites image

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.

Absolute positioning in WPF

I have a long text and show first sentence in a TextBlock.
I wish by clicking the TextBlock or a button to show a panel below the TextBlock with full text. I wish this panel be absolutely positioned and be displayed above any other elements, you can do a similar thing in HTML showing and hiding absolutely positioned 'div' element.
How to do this in WPF?
Thank you for any suggestions.
AdornerLayer can work, but may be a little complex. Other options include using PopUps or ToolTips -- you should look into those first as your easiest options.
If these all don't work, it'll really depends on what kind of panel you're using. For example, if you're using a Canvas, all you have to do is make sure to set the correct ZIndex on the element.
In order to make this more robust, I'd suggest the following:
<!-- Set Panel.ZIndex="99" when showing hidden area to ensure top placement -->
<Grid>
<TextBlock>This is my primary bit of text ...</TextBlock>
<!-- Canvas stays hidden until we want to show the rest of the text -->
<Canvas Visibility="Hidden">
<TextBlock Canvas.Bottom="-10">Content goes here</TextBlock>
</Canvas>
</Grid>
Put the long text in an AdornerLayer is the best option. Check out some links
http://msdn.microsoft.com/en-us/library/ms743737.aspx
http://wangmo.wordpress.com/2008/10/19/relations-between-adorner-adornerlayer-and-adornerdecorator/

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