I have a canvas in which I add a child image element.
<Canvas SnapsToDevicePixels="True" Width="56160" Height="37440">
<Image Width="56160" Height="37440" x:Name="OverlayImage"/>
</Canvas>
On this canvas, I need to draw ordinary lines that will blur the background. An example of this brush is available in Photoshop (Blur brush).
I know how to blur the entire canvas using the BlurEffect class (from the System.Windows.Media.Effects namespace).
But I need blurring where the user draws the line.
I guess that maybe I need to implement a shader for this task, but I don’t have the practice of writing code HLSL (High Level Shader Language).
Tell me, what approach should I use to solve this problem?
Blur the whole image, store the result, use a regular brush and paint the mask and alphablend the blurred image with the original using the mask.
Related
I've been searching for awhile, but haven't been able to find anything. I'd like to be able to add kind of a glimmer or sparkly animation on an image element in wpf.
Essentially the effect here I'm after here is the same that you get with trading cards that are "foil's".
I'd like to have an image, and then be able to add this animation to it at will. I'm thinking maybe some kind of user control, or template possibly. Hopefully generic enough that I can just toss an image at it and it will just overlay the image and run.
Any ideas?
A simple construction that easily can be turned into a control is by nesting the image in a Grid and adding a second Grid (on top) as a sibling.
De second grid can be given a linear gradient brush that is primarily transparent but does contain a white glimmer.
This brush can be animated; you could move it and change the opacity of the grid/brush.
This way you do not change the image.
I am writing a WPF app that displays an image that is initially centered. The user can zoom in/out and move the image, which are implemented using ScaleTransform and TranslateTransform. That works great.
The problem is when the image is significantly bigger than the window, and the user moves the image or zooms out enough so that the entire image should be visible. The portion of the image that was originally hidden isn't rendered, and instead only the originally viewable part of the image is drawn.
Based on some other questions, if I put my image inside of a Canvas that will cause the entire image to be rendered, and when moved it will be rendered correctly. The problem is that I don't want my image to be in a Canvas, since that prevents any other layout from occurring - the HorizontalAlignment and VerticalAlignment properties are ignored (so the image is no longer centered), and I need to implement an option that will draw the image as large as possible to fill the entire area of the window which no longer works (setting the Stretch property to UniformToFill doesn't do anything).
Currently the two transforms are set to the RenderTransform property. If I use LayoutTransform instead, the entire image is drawn, but this also prevents the user from moving any portion of the image off the edge of the window (which is behavior that I would like to keep).
How can I tell WPF to always render the entire image without using a Canvas or a LayoutTransform?
I suggest not using a TranslateTransform but instead rely on the ScrollViewer.
<ScrollViewer>
<Grid>
<Image Source="blabla">
<Image.LayoutTransform>
<ScaleTransform />
</Image.LayoutTransform>
</Image>
</Grid>
</ScrollViewer>
The ScrollViewer gives the Image infinite space to expand, the entire image will be rendered. The Grid forces a centered layout while still allowing the image to expand. If you don't like the scrollbars to control the translate then you can hide them and roll your own solution.
LayoutTransform is definetely the way to go, so that the image's actual size in pixels (based on the zoom) is properly reflected onto your window.
Ok, I've created a PNG-24 with transparency. It's basically a grayscale image that uses 'colors' in between black and transparent instead of black and white. I did this so I can use this as the Opacity Mask of a colored rectangle, thus rendering the image in whatever color I want using only a single graphic.
However, for the life of me, I can't get WPF to stop anti-aliasing the da*n image!!
I've set 'SnapesToDevicePixels' on the rectangle to which the brush is applied... I've set the ImageBrush's Scale to 'None'... I've set its ViewPort and the ViewBox to absolute units and sized them exactly to the source image. But no matter what I try, WPF still insists on trying to smooth things out! This is VERY frustrating!!!
So... anyone know how to use an image as an opacity mask but not lose the pixel-precise drawing that we have done? I just want WPF to render the damn thing as we drew it, period!
I have tried to reproduce your problem. Simply like this:
<Rectangle Width="200" Height="200" Fill="Red">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="/mask.png"/>
</Rectangle.OpacityMask>
</Rectangle>
mask.png contains a simple diagonal mask, like that half of rectange is visible and other half is 100% transparent.
And recrangle is rendering pixel perfect (and aliased, as you want).
I think, that you may a DPI setting, that is not native to your monitor, and WPF just can`t render images correctly.
GOT IT! It's a layout issue that for some reason, there's no easy way to change. However, there's a value you can set called UseLayoutRounding that fixes it. I just set it at the root level (for this fauxample, a grid...)
<Grid UseLayoutRounding="True">
....
</Grid>
...and BAM! Works like a charm! "Sort of" like a 'SnapsToDevicePixels' but for positioning of elements (i.e. it rounds all layout-related values like left, width, etc. whereas SnapsToDevicePixels snaps the layout to the on-screen pixels when rendering.)
M
Back in Win32 days, if I had a monochromatic bitmap, which is for all intents and purposes just a bitmap mask, I could render that bitmap onto a DeviceContext. What would happen is every pixel where there was a '0' it rendered as transparent, and every pixel where there was a '1' it rendered in the currently selected forecolor. This was very handy.
However, after a few weeks of searching, I haven't seen anything similar in WPF.
Now I was about to just roll my own MonochromeBitmapImage class, but thought if anyone would know another way to do this, it would be you all, the S.O. users, so there's my question... how, in WPF, can I render a monochrome bitmap with one value being a specificed color and the other value being transparent?
A pretty similar feature is OpacityMask for UIElement. It will display the parts that isn't transparent in the OpacityMask. It can be used with
Brushes - SolidColorBrush etc
Visuals - VisualBrush
Images - ImageBrush
etc
Here is an example with a Button using an ImageBrush
<Button Content="Opacity Mask">
<Button.OpacityMask>
<ImageBrush ImageSource="C:\OpacityRect.png"/>
</Button.OpacityMask>
</Button>
It depends if you need to use monochrome bitmaps or you just want to be able to recolor the image. If you can use any image format I would use png with alpha then you can use the image as an OpacityMask, no code necessary. By using the image as an OpacityMask you can easily change the colour, use a gradient or an ImageBrush.
<Rectangle Fill="Blue">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="..." />
</Rectangle.OpacityMask>
</Rectangle>
If you need to use 1-bit bitmaps it's quite simple to convert them to an indexed format with a custom palette. You could set the colour you want in the palette or just choose anything and use it as an OpacityMask. FormatConvertedBitmap seems to need a different format just a different palette had no effect.
public static FormatConvertedBitmap Recolor(BitmapImage b, Color c)
{
return new FormatConvertedBitmap(b, PixelFormats.Indexed2, new BitmapPalette(new List<Color>() { Colors.Transparent, c }), 0);
}
Another option would be a PixelShader it's a bit overkill for this kind of thing but for more complex manipulations they useful.
The System.Drawing.Graphics class has a property CompositionMode with two options: SourceOver (which, based on the alpha component, blends whatever is drawn with the background already existing) or SourceCopy which simply overwrites the background with whatever is being drawn.
Does something similar exist in WPF?
In WPF when i draw a PolyLine for example on top of another the new PolyLine always alphablends with the background. I think that is independent of the container being used. I am using a Canvas but could not find a blend mode property anywhere. What I want to do is what the SourceCopy compositionmode mentioned above does. I.e. the new PolyLine should simply overwrite whatever is already on the Canvas.
Is there a simple way to do that, short of using pixel shaders (which - as far as I understand - wouldn't work anyways because I don't have access to the Canvas backbuffer).
I am not stuck with a Canvas and would be happy to use any container that supports overwrite mode.
I currently have a solution based on a WriteableBitmap for which I obtain a System.Drawing.Graphics context and then manipulate the CompositionMode. It works but since my window is fullscreen that solution has serious performance impacts.
Clarification and example:
The WPF window is fully transparent and so is the Canvas (back ground color(0,0,0,0)). Now I draw a PolyLine with a Color.FromArgb(128,128,0,0). I now have a semi-transparent red polyline. Next I draw the same PolyLine with Color.FromArgb(0,0,0,0). The result is the same as before because of the alpha blending taking place. What I want, however, is that the red polyline is erased with the second polyline (which is exactly what the SourceCopy mode in the Graphics class does.
I think all you need to do is make sure that the brushes used to fill/stroke the PolyLine have fully opaque alpha values (i.e. 255). Then the background shouldn't be blended into it.
You could apply a Clipping Mask, this way you can provide the path to clip over the elements that are below it, but it might be tough to maintain after a lot elements are required to be clipped...