This question already has an answer here:
Drop Shadow effect in Universal Windows Application
(1 answer)
Closed 5 years ago.
I am currently trying to create a circular button with two ellipse-elements in UWP and want one of them to throw a shadow at a certain angle. I found a way to do so in WPF which looks like this:
WPF XAML:
<Ellipse>
<Ellipse.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Direction="-50" ShadowDepth="50" Softness=".7"/>
</Ellipse.BitmapEffect>
</Ellipse>
What's the equivalent in UWP?
The easiest way is to use the DropShadowPanel from UWP Community Toolkit.
So first just install
Install-Package Microsoft.Toolkit.Uwp.UI.Controls -Version 2.0.0
Then use the following code in your XAML
<controls:DropShadowPanel Color="Black"
OffsetX="-50"
OffsetY="-50"
BlurRadius="50"
ShadowOpacity=".7"
Width="120"
Height="120"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<Ellipse />
</controls:DropShadowPanel>
In UWP there is a different component to do this job. It's called the Composition API and is available in the NuGet Package "Win2D.uwp".
Basically you'll need to get the compositor for your visual object with
_compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
and create a drop shadow using the compositor.
_compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
// create a red sprite visual
var myVisual = _compositor.CreateSpriteVisual();
myVisual.Brush = _compositor.CreateColorBrush(Colors.Red);
myVisual.Size = new System.Numerics.Vector2(100, 100);
// create a blue drop shadow
var shadow = _compositor.CreateDropShadow();
shadow.Offset = new System.Numerics.Vector3(30, 30, 0);
shadow.Color = Colors.Blue;
myVisual.Shadow = shadow;
// render on page
ElementCompositionPreview.SetElementChildVisual(this, myVisual);
The downside beeing, that this is not quite straight forward. You can use different brushes to display images, solid colors or other stuff, it won't apply to existing visuals on screen (as far as I understand it). You can read more about the basics here. Probalby you'll need to use a surface brush, which can hold a wide variety of different visual types, like images. Currently it does not look like there is a ready made component for ellipses though.
Alternatively there exists a xaml extension which will do all that stuff for you using pure xaml, might be worth a shot and maybe also support ellipses.
As an ending note, all of this is currently a work in progress on microsofts part and should become a native part of the UWP API in the future.
Related
We're having a requirement to build an OS independent application where we need to draw Rectangles, Lines and Ellipse.
We are developing in a ASP.Net Core (MVC) application to realize the OS independence.
Our ideal situation is to prefix the drawing objects in a model class (height, width, color) and request the objects in the controller or razor view to show them on the web page.
We've searched on the internet and found several options with a BitMap of System.Drawing.Common.
The most common solution is a form application witch we want to avoid, because it isn't OS independent.
What is the best suggestion to realize our situation?
We are not afraid to try the combination of C#, JS HTML and so one, only requirement it needs to fit in the ASP.Net Core application.
Have a look at fabric.js for interacting with the html canvas.
You can develop directly with the canvas, but it will be much easier to use a library that already handles all the heavy lifting.
see: http://fabricjs.com
What about <canvas> ?
The HTML <canvas> element is used to draw graphics on a web page.
var c=document.getElementById("myCanvas");
var canvOK=1;
try {c.getContext("2d");}
catch (er) {canvOK=0;}
if (canvOK==1)
{
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(20,20,100,50);
var grd=ctx.createLinearGradient(140,20,240,70);
grd.addColorStop(0,"black");
grd.addColorStop(1,"white");
ctx.fillStyle=grd;
ctx.fillRect(140,20,100,50);
var grd2=ctx.createLinearGradient(20,90,120,90);
grd2.addColorStop(0,"black");
grd2.addColorStop("0.3","magenta");
grd2.addColorStop("0.5","blue");
grd2.addColorStop("0.6","green");
grd2.addColorStop("0.8","yellow");
grd2.addColorStop(1,"red");
ctx.fillStyle=grd2;
ctx.fillRect(20,90,100,50);
ctx.font="30px Verdana";
var grd3=ctx.createLinearGradient(140,20,240,90);
grd3.addColorStop(0,"black");
grd3.addColorStop("0.3","magenta");
grd3.addColorStop("0.6","blue");
grd3.addColorStop("0.8","green");
grd3.addColorStop(1,"red");
ctx.strokeStyle=grd3;
ctx.strokeText("Smile!",140,120); }
<canvas id="myCanvas" width="270" height="160" style="border:1px solid #c3c3c3;float:left;margin-right:20px;margin-bottom:15px">
Your browser does not support the <canvas> element.
</canvas>
The graphic to the above is created with ''. It shows four elements: a red rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text.
What is HTML Canvas?
The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript.
The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
for complete view
https://www.w3schools.com/html/html5_canvas.asp
https://stackoverflow.com/search?q=canvas
I am trying to achieve a similar objective in an asp.net core 2.0 project. From my ".cshtml" razor view, I am calling a Typescript routine to handle to GUI complexity. Within the view, I am using a HTML5 canvas on which I want to draw rectangles over a background image.
[jsFiddle][1] seems to be the best website to understanding the various interaction of these aspects (HTML, CSS, TypeScript), specifically these examples:
Creating circles with a mouse
Circle drawing in fabricjs
Currently, I not 100% sure how to include "Fabric.js" within the project "wwwroot" directory (or other locations within the project) and secondly how to maintain the background image throughout the drawing of circles, lines etc.
This seems to align with all the responses above. I do know that "System.Drawing" are not available within ".NET Core 2.0" (even via a NuGet package), so unfortunately other methods need to be pursued.
I am using the 1st release of the ArcGIS Runtime SDK for .Net - Xamarin.Forms (nuget package here).
One of my requirements is to display a basic scale line on the map. I haven't found any build-in feature for the moment.
It seems to be tricky because each device has different size, different resolution... Any idea on how to implement this ?
OK after few hours, I found that the MapView component has a property UnitsPerPixel that do exactly what I needed:
I've added a small grid (to represent the scale) with a fix width:
<Grid HeightRequest="10" WidthRequest="114" x:Name="Legend">
...
</Grid>
Then when the view point changes, I compute the distance representing by this grid:
MapView.ViewpointChanged += (sender, args) =>
{
ScaleVal.Text = $"{Math.Round(Legend.Width * MapView.UnitsPerPixel, 0)}m";
};
Complete solution here.
I am trying to get a feel for the best practice for FormState icons(or icons in general) on a WPF form.
The reason I ask is because when I originally created my Min/Max/Restore/Close buttons I created a few different implementations thinking one would be obvious when they were done. I first created them using the same method as modern UI by MahApps. Using datapoints in the xaml to draw them. I then created my own in illustrator by tracing them from Visual Studio 2012 and then saved them as SVG's. My third approach was after another mentioned they were using the Merlott font in house.
After looking in to using the merlott font I found an answer on the same topic. The answer said that using the Merlott font was best practice and to avoid using the path data points. The answer can be seen here: Making WPF applications look Metro-styled, even in Windows 7?
So this made me even question it even more. What I decided on was using the Scalable Vector Graphics(SVG format).
From there I was able to convert the graphic in Blend Design into pure XAML. It renders the shapes using geometry.
So at this point I have 4 different ways of completing this task. Each one around the same difficulty.
Scalable Vector Graphics - Retraced the shapes in illustrator and exported to pure scalable vector graphics and use the .svg as a resource.
Geometry/XAML - Converted the SVG directly to XAML. This implementation uses geometry to render the shapes.
Path Datapoints - Uses another XAML approach to draw the icons.
Windows Font(Merlott) - This has been around for ages and some think this is the best practice. Normally I would think this isn't a viable option unless including font with project, but Merlott is installed on Windows by default.
So this leaves me with quite a bit of confusion. I have these 4 implementations, all easy enough to implement and no idea which one is the best practice.
Some may think this is subjective, and possibly anal. Although I would like to use best practices on this project(and future projects).
Could anyone care to explain which one would be the better option, and why?
It seems that this tool is perfect for you:
https://github.com/BerndK/SvgToXaml
It can browse svg files and can convert them (without using other tools like inkscape or illustrator). It can also create a single xaml file for all svgs in a folder (batch). The you just have to refer the created object like this and you are golden:
<Button>
<Image Source="{StaticResource cloud_3_iconDrawingImage}"/>
</Button>
Sample app is included.
To answer your question (4 options?)
Option 1 is not possible out of the box (a solution could be https://sharpvectors.codeplex.com/)
Option 2 and 3 seems to be identical - the Path-Object uses a Geometry. To be more precise I prefer using an Image not a Path, because Image has clipping and can contain several PathGeometries with several Colors. The Image can handle MouseClicks better than a Path.
The result can look like this:
<DrawingImage x:Key="cloud_3_iconDrawingImage">
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V512 H512 V0 H0 Z">
<GeometryDrawing Brush="#FF000000" Geometry="F1 M512,512z M0,0z M409.338,216.254C398.922,161.293 350.672,120.477 293.557,120.477 258.459,120.477 225.926,135.762 203.686,162.061 166.538,152.155 127.607,173.842 116.753,210.84 78.16,222.176 50.6,257.895 50.6,299.303 50.6,350.155 91.97,391.524 143.822,391.524L369.18,391.524C420.03,391.524 461.401,350.155 461.401,299.303 461.4,263.389 440.941,231.457 409.338,216.254z M369.18,351.523L143.821,351.523C114.026,351.523 90.599,328.097 90.599,299.302 90.599,265.224 118.249,239.224 152.785,245.486 141.249,205.89 196.916,183.556 217.426,213.138 222.583,198.556 243.249,160.476 293.557,160.476 331.584,160.476 370.918,186.556 372.221,245.458 397.584,245.556 421.401,263.89 421.401,299.302 421.4,328.098 397.975,351.523 369.18,351.523z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
Note this is the definition of the "Image/Icon" to be stored somewhere in the resources. To show it use it as ImageSource with an image as shown above.
Option 4: I think there are only some icons given there, not my preferred solution, but perhaps I understood your idea with the font wrong.
Just a simple question: I'm developing a Windows store app and for one function I want to show an intercept theorm. For that (now my question) I need some lines. Do I need to create an Image or is there any other possibility to display simple lines on a XAML-Form (I'm using XAML and C#).
I'm coming from Windows forms and there I used a line control from the Toolbox, but I can't find anything like that in the Toolbox of VS for Windows 8. I also had the idea to use GDI, but I read that it doesn't exist any longer (am I wrong?).
You should draw shape elements, in your XAML (or code-behind).
See MSDN: http://msdn.microsoft.com/en-us/library/windows/apps/hh465055.aspx
you can use like bellow
<Line X1="0" Y1="0" X2="100" Y2="100" Stroke="Red"></Line>
this draws a line from (0,0) to (100,100).
don't forget to use stroke otherwise it won't be displayed.
I'm now developing an app with reflect effect. I tried to assign a VisualBrush to Rectangle.Fill as:
<Rectangle.Fill>
<VisualBrush Opacity="0.75" Stretch="None" Visual="{Binding ElementName=ReflectedVisual}">
</VisualBrush>
</Rectangle.Fill>
And VS reports VisualBrush doesn't exist in my xml namespace. I manually added it to the XAML file using:
xmlns:fx="http://schemas.microsoft.com/netfx/2007/xaml/presentation"
And added the reference DLL as well. However, now VS says that I cannot assign a VisualBrush to a property of class Brush.
This seemed weird to me, as I recalled the same code worked well on Vista. Does anyone know if there's anything I'm missing here?
Thanks.
Metro apps do not have the same set of XAML brushes, resources and elements available as in WPF.
A work-around would have been to use a WriteableBitmap and use the Render method to draw the element to the bitmap. Unfortunately the current version does not support the Render method.