Difference between Silverlight and WPF for TextBlock.BaselineOffset - wpf

I'm using a shared code to produce documents in Silverlight and WPF. But the output is different for the TextBlock alignment.
For example for a TextBlock of height 100, displaying a text in Arial with a FontSize of 100 :
In WPF the BaselineOffset is 92.16333
In Silverlight the BaselineOffset is 90.52667
This difference depends on the font family, for example if I replace Arial by Times New Roman :
In WPF the BaselineOffset is 91.23666
In Silverlight the BaselineOffset is 89,11
Is there a way to correct this behaviour and have the same alignment in WPF and Silverlight ?
Thanks for your help

Having worked in both WPF and Silverlight, I can confirm the rendering output of the same fonts at the same font size the output looks significantly different on screen.
In WPF you have a bit more control using RenderOptions i.e.:
RenderOptions.SetBitmapScalingMode(tb, BitmapScalingMode.NearestNeighbor);
RenderOptions.SetClearTypeHint(tb, ClearTypeHint.Auto);
RenderOptions.SetEdgeMode(tb, EdgeMode.Aliased);
You may need to play with values above to get as close as possible. Also as the renderoptions API is WPF only, if you're in a shared codebase you'd need to wrap it in a #if WPF directive..

Related

Nine-Patch Image in Windows Phone

In Windows Phone UI Design Principle, MS recommended use solid color rectangle or coding-gradient for Control Background to avoid incompatible in multi-screen. But in many requirements, using image as Control Background is necessary. Then, 9-patch image technique is used. In Android and IOs, it was support in core, but in WP it is lacking. I try to use it in WP by 3 approaches:
Using 9-cells Grid: clip image into 9 patch and lay them into cells. It works ok, but i afraid app performance reduce when has many control.
Using Custom Brush: only custom Brush to draw 9-patch image as ImageBrush, but seem MS not allow for custom Brush.
Using FramworkElement: like Rectangle, Ellipse... i want to create a FrameworkElement can draw a 9-patch image. But, can't use low-level render.
How can i implement 2nd and 3th approach?
I created a lib for Windows Phone which do exactly as Android NinePatchDrawable. You just need to set a bitmap image.9.png, the width and heigh... And done!!! you have you new image scale to the size you want. Enjoy it :). In the future I will add more option :).
GitHub link
You can compensate for the lack of low-level rendering and custom brush by using a WriteableBitmap: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap(v=vs.95).aspx
This way, you have complete control on how to render your background, then you can assign it to a single Image control. But it's way more complicated than the "use a grid with 9 image controls" method, and the performance improvement is probably insignificant.

Custom Control - Why do I have a garbled Textblock visual artifact?

I am developing a custom control in WPF/C# that involves a scale. The scale shows numeric labels (TextBlock instances) at key ticks along the scale as one would expect.
When doing some manual testing I came across a strange scenario where if I have just the right size of control a particular scale label becomes garbled.
Here is a link to a screenshot:
If I re-size the control a single pixel then it all works as expected.
Any idea what is happening? Is it just a WPF bug?

How to prevent WPF from scaling with the Windows font size options?

I don't want my WPF GUI to scale with Windows font size options (DPI). It's not just a matter of specifying a fixed font size on the UserControl because scaling affects images and borders in the UserControl. Is there a property that I can set on the UserControl to turn off scalling? If not, how can I do that?
You could scale down your UserControl based on the current DPI setting. For example, if you wrapped your UserControl with the following DpiDecorator, then it should look the same regardless of the DPI setting:
public class DpiDecorator : Decorator {
public DpiDecorator() {
this.Loaded += (s, e) => {
Matrix m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
ScaleTransform dpiTransform = new ScaleTransform(1 / m.M11, 1 / m.M22);
if (dpiTransform.CanFreeze)
dpiTransform.Freeze();
this.LayoutTransform = dpiTransform;
};
}
}
Or you could move that logic to your UserControl.
The code to obtain the DPI scale factor was from this blog post.
Windows includes a compatibility helper for buggy applications that fail under high-dpi settings:
Be sure to leave off (or turn off) "Use Windows XP style DPI scaling":
And be sure your application does not have a "dpiAware" entry in its assembly manifest.
Windows will lie to your application, tell it that it's 96dpi, and then the graphics card will scale the entire window up for you.
Everything will be slightly fuzzy, and generally unpleasant to use, but it will work well enough until you can fix the buggy WinForm control.
Note: The dpiAware manifest entry lets your application tell Windows that it wants to opt-out of the dpi-scaling. Applications only add this item if they've been tested at high-dpi.
in addition the the DpiDecorator, you will also need to fix the font size of your items. For example, in Windows XP, if your setting is set to Large Fonts, Menu Item font size is set to 14 and is also scaled up using the DPI setting, so if you don't fix your MenuItem font size or any other UI item font, you will get Window's default value for this item type. Also remember that a user can manually change the font size/font face for other items...

How do you customize a WPF TextBox control (template) appearance?

TextBox seems to work differently from others...
Say you want a different font, rounded corners, different colors, a tiled image for the background, reduced padding above/below the text (when I change the font on an individual TextBox they get a little too tall but there's a lot of wasted space), etc. How do you do this in Blend 4?
I'd like it to be in App.xaml so that it's used by default for all TextBox controls in the app.
I think everything you mentioned can be done using controltemplates.What is not working ?
Check this link
http://msdn.microsoft.com/en-us/library/ms752068%28v=vs.85%29.aspx
It will get you started for sure

WPF Tiled Background Misalignment

We have quite a complex WPF application (that I cannot show here) that somehow has the tiled background misaligned of one of its user controls. I was unable to reproduce this problem in a "clean" WPF project but will try to illustrate the problem in this picture:
(source: kintespace.com)
The gray area represents the user control and the black and red checks represents the tiled background inside the user control. The white background represents the window hosting the control. The VisualBrush is used here but the same problem comes with the DrawingBrush. I would prefer not to use the ImageBrush.
Can you open a "clean" WPF project and build something that will cause a tiled background to be misaligned like this?
You can do this if the layout root of the user control has a -10 left margin and a -10 top margin. the tiled backgoround in on the layout root of the usercontrol so, it looks like a alignment problem not a brush problem.
This problem is related to the tiled background cramming itself into the calculated height of the UserControl instance in the hosting Window. I am still unable to replicate this problem in a new project but it is "solved" (by stacking panels in the user control horizontally instead of vertically).
Here are two diagnostic techniques that can be useful in situations like yours:
Take your complex project, make a copy, and start tearing out large sections of code and XAML until the problem disappears. Then put the last section back and tear things out more gently. Repeat until you find the change that makes the difference.
Run the application, break where a local variable references your UserControl, then explore the visual tree just above and below your UserControl in the Locals or Watch window. Look at each Visual's internal properties VisualOffset, VisualTransform, and VisualContentBounds. These properties will usually clue you in to what property is being set incorrectly, and from there you can figure out why.

Resources