WPF dashed line — fix dashes during resize - wpf

How can I avoid line dash resize during the window resize? Consider the following image:
http://leprastuff.ru/data/img/20130315/b83eea4a7a3f07ca53a0e118ddbb9230.Gif
<Rectangle SnapsToDevicePixels="True"
Stroke="DarkGray"
StrokeDashArray="10 10"
StrokeThickness="1" />
Thanks,
Tony.

Set the Height of the Rectangle to something greater than one.
Currently you're drawing the upper and lower border on top of each other. As the dashed stroke draws the Rectangle's border clockwise, starting at the top-left corner, you get the observed behaviour.
If you intended to draw a Rectangle with a height of one, you might better draw a Line instead.

Related

Hide UIElement but display that element's Effect

I am drawing a sequence of lines with DropShadowEffect in WPF. I want all the effects to display behind all the lines. So I drew the sequence twice: The first time I set Line.Stroke to Transparent and added the effect, and the second time I set Line.Stroke how I wanted and left off the effect. Unfortunately, a transparent stroke seems somehow to hide the effect as well, so my first sequence of lines doesn't display anything. Setting StrokeThickness to 0 also hides the effect. How can I show Line.Effect without showing the Line itself?
As commented, WPF - Element is invisible but has dropshadow effect asks the same question I do, but I want a code solution if possible, not just a Blend feature.
A dropshadoweffect is a sort of gaussian blur.
It takes pixels and sort of copies them offset.
You need something to copy.
So the answer is no, you cannot.
You could maybe try clipping the line to remove it, but I would think that's going to give you a hole where the line was.
In fact if you take the question as stated then the shadow is a blurred version of the line offset by a few pixels.
If your line is say:
<Line X1="0" X2="100" Y1="0" Y2="150"
StrokeThickness="5"
Stroke="Black">
<Line.Effect>
<DropShadowEffect/>
</Line.Effect>
</Line>
The shadow is something like:
<Line X1="4" X2="104" Y1="3" Y2="153"
StrokeThickness="5"
Stroke="Black">
<Line.Effect>
<BlurEffect/>
</Line.Effect>
</Line>
If this is not exactly what you want I could give you code which does a box blur on a black white image.
It's not as neat as gaussian but gaussian is hard to code. You'd have to paint your lines then blur em.
For interest
This is an explanation of the gaussian algorithm:
https://blog.en.uwa4d.com/2022/08/11/screen-post-processing-effects-chapter-1-basic-algorithm-of-gaussian-blur-and-its-implementation/#:~:text=Gaussian%20Blur%2C%20also%20known%20as,pixel%20values%20in%20the%20field.

Understanding Stroke and StrokeThickness in relation to Height

Below I have a rectangle:
<Rectangle ClipToBounds="False" Grid.Row="1"
StrokeThickness="6"
StrokeDashArray=".5"
Height="2"
Stroke="Green" />
In the definition above, I have observed that to see the effect of StrokeDashArray=".5", I have to maintain the StrokeThickness between 6 to 9. If I give anything below 6 or above 9, I see a straight line.
I have two questions here:
In the above example, StrokeDashArray effect does not work below 6 and above 9. Why?
I think there is a relation between height and StrokeThickness. Can someone please explain?
When I read the MSDN Definition on stroke thickness it says:
Gets or sets the width of the Shape outline
I do not understand this.
Thanks in advance.
The stroke dash array is not expressed in pixels, it's dependent upon stroke thickness to get the final pixel value for each number in the array.
look here if you want to understand all the details behind it.
Generally speaking: The StrokeThickness tells WPF how thick
the line should be in pixels.
StrokeDashArray is a property that allows us to define the dashes and gaps in a
line. The values in the array alternate between the length of the dash and the
length of the gap. The values are also relative to the thickness of the stroke, so a
value of 1.0 means that the length of the corresponding dash or gap is equal to the
width of the stroke.
In your code you tell WPF to draw a dash that is 0.5 times of the width of the stroke but you didn't give the length of the gap, for example:
StrokeDashArray=".5 1"
This example says that the gap is equal to 1 time of the width.
Buttom line you need to define the gap for getting the wanted gap between the dashes.

How to make some layer mask in Silverlight?

I have an image object and a rectangle object. Now I want to be the image to be only visible where the rectangle is, everything else should have an opacity of 0.5 - the result should look something simliar to the following:
When I set the opacity of the rectangle to 0.5 the effect is the antipode - so how could I realize it as shown in the image? Size and position of the rectangle is changed by code-behind, but that shouldn't make any differences...
All hints / answeres appreciated :)
If you want to add an opaque mask to partially hide your image outside of a rectangle, it's rather easy.
<Grid>
<Image Source="myImage.jpg" Opacity="0.5/>
<Image Source="myImage.jpg" >
<Image.Clip>
<RectangleGeometry Rect="x,y,w,h"/>
</Image.Clip>
</Image>
</Grid>
Where x,y,w and h are your rectangle position and size (see MSDN). If you want to move the visible portion around, set the Clip property by code.
If you want to add the "black stroke effect", you could simply add a Rectangle with position and size matching those of your clipping path after the second image in your grid.
Do you want more infos?

WPF: How do I create a background that repeats horizontally without scaling?

I would like to create a background for my window which is an image that I want repeated horizontally. So far I've tried with the ImageBrush, but this option repeats the image horizontally and vertically. Also, I don't want it to scale when user resize the window, as it makes the image look funny.
If what you want to do is tile an image horizontally as you would in CSS with the simple one liner "background-repeat: repeat-x" then after some (!) trial and error what you need in XAML is this:
<ImageBrush ImageSource="Images/my-background-image.png"
TileMode="FlipY"
Stretch="Uniform"
AlignmentY="Top"
Viewport="0,0,90,3000"
ViewportUnits="Absolute" />
Where the last 2 values on the Viewport attribute are the width of your image in pixels and then a very large number that is higher than your viewport height so that the image is not repeated in the Y direction within that height.

Silverlight: Canvas overflows

I have created a Canvas, and within it I placed a StackPanel. The StackPanel is horizontal, and it accepts a list of thumbnailed images. The Canvas has a fixed size. When I put more thumbnails than the Canvas width can hold, the StackPanel is supposed to overflow from the Canvas, so I can move it to center the current thumbnail.
Everything works correctly, only, the StackPanel's overflow is visible! Is there a way to hide it? Or is the entire approach wrong?
Here is a screenshot. The canvas is the red box. The stackpanel is blue semi-transparent.
http://www.netpalantir.it/static/sl_canvas_overflows.jpg
Thanks!
Since the Canvas has fixed size, you can use clipping. Basically you have to do:
<Canvas Width="400" Height="300">
<Canvas.Clip>
<RectangleGeometry Rect="0, 0, 400, 300"/>
</Canvas.Clip>
<!-- your StackPanel here -->
</Canvas>
Here are few useful posts on the topic:
Clipping in Silverlight
Cropping or Clipping in Silverlight

Resources