SVG graphics auto scaling in J2me - mobile

i am developing my first application using SVG graphics , what about auto scaling of SVG background image to match the device screen width and height ? is that possible or shall I have multiple files for the different display resolutions

I'm not sure about J2ME specifically, but this is typically accomplished by setting the viewBox attribute on the root SVG element, and setting the width and height to 100%. See here for an example:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1"
viewBox="0 0 1500 1000" preserveAspectRatio="none"
xmlns="http://www.w3.org/2000/svg"
width="100%" height="100%">
<desc>Example ViewBox - uses the viewBox
attribute to automatically create an initial user coordinate
system which causes the graphic to scale to fit into the
viewport no matter what size the viewport is.</desc>
<!-- This rectangle goes from (0,0) to (1500,1000) in user space.
Because of the viewBox attribute above,
the rectangle will end up filling the entire area
reserved for the SVG content. -->
<rect x="0" y="0" width="1500" height="1000"
fill="yellow" stroke="blue" stroke-width="12" />
<!-- A large, red triangle -->
<path fill="red" d="M 750,100 L 250,900 L 1250,900 z"/>
<!-- A text string that spans most of the viewport -->
<text x="100" y="600" font-size="200" font-family="Verdana" >
Stretch to fit
</text>
</svg>
Live demo here: http://stuff.echo-flow.com/stackoverflow/viewbox.svg
For information on preserving the aspect ratio of the image, see the specification here: http://www.w3.org/TR/SVG/coords.html#PreserveAspectRatioAttribute

Related

React Native Mask Image with another image

I am trying to mask an image with a shape using the react-native-svg library but things don’t work pretty well on android (clipPaths do not support transforms) so I am looking how to mask the image with another image let’s say a png assert that has a black shape in it.
Has anyone achieved something like that?
I finally managed to do this by using the viewBox property and correctly setting the sizes of the underlying paths and elements. The viewbox size is the size of the path I use to do the clipping. imageWidth & imageHeight are the final dimensions I want to have for the clipped image (and the mask)
<Svg width={ imageWidth } height={ imageHeight } viewBox='0 0 320 224'>
<Defs>
<ClipPath id='clip'>
<Path d={ path } />
</ClipPath>
</Defs>
<Image
width='100%'
height='100%'
preserveAspectRatio='xMidYMid slice'
href={ { uri: filePath, isStatic: true } }
clipPath='url(#clip)'
/>
</Svg>

In WPF what is the proper method for drawing a line and PNGs on a bitmap that is attached to a canvas?

I don't do that much with WPF anymore and I always seem at a loss for the most basic things. I've tried the Googles but they're not helping.
I've got a canvas (maybe I shouldn't use a canvas?) and I want to attach an image. The only way that I could find to do this was like so:
<Canvas Grid.Column="2" HorizontalAlignment="Right" Height="822" VerticalAlignment="Top" Width="1198" Name="MainCanvas">
<Canvas.Background>
<ImageBrush ImageSource="/MapDesignModule;component/MapFrame.bmp" Stretch="None" AlignmentY="Top" AlignmentX="Right"/>
</Canvas.Background>
</Canvas>
Now, I need to draw lines on the image attached to the canvas (later I will also have to place transparent PNGs, or BMPs with white set to Alpha 0, on the image as well).
In the past I would get a writeablebitmap from the image.source but you apparently can't do that from an ImageBrush?
What is the 'proper way' to put an image on the screen and draw and blit images onto it?
Just put multiple Image and Line elements on top of each other in a common Panel, e.g. a Canvas:
<Canvas>
<Image Source="/MapDesignModule;component/MapFrame.bmp" />
<Image Source="/MapDesignModule;component/Overlay.png" />
<Line X1="100" Y1="100" X2="200" Y2="200" Stroke="Black" />
</Canvas>
You could also assign a name to the Canvas
<Canvas x:Name="canvas">
to access it in code behind:
canvas.Children.Add(new Image
{
Source = new BitmapImage(new Uri(
"pack://application:,,,/MapDesignModule;component/MapFrame.bmp"))
});
canvas.Children.Add(new Line
{
X1 = 100,
Y1 = 100,
X2 = 200,
Y2 = 200
});
In case you later want to add multiple shape overlays, you may consider using an ItemsControl with an appropriate view model, e.g. as shown here: https://stackoverflow.com/a/40190793/1136211

Xaml, wpf image position and crop issue

I have images of 600px width and 600px height. we have three sizes of circles. all have the center in the middle. Some have reflection as shadow beneath it. I would like to crop the image for display purposes.
So the largest circle as shown above has a diameter of about 500 pixels, but the medium and small ones have less. I know in the code which size I have of object type Product. Because of the size differences I have to place them differently and used three placeholder images for it, like this:
<Image x:Name="imgCoinHolderSmall"
HorizontalAlignment="Center"
Margin="0,495,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
<Image x:Name="imgCoinHolderMedium"
HorizontalAlignment="Center"
Margin="0,510,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
<Image x:Name="imgCoinHolderLarge"
HorizontalAlignment="Center"
Margin="0,520,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
So can I change the properties of the image such that it does not display the red part of this screenshot:
By the way, I do not display the images on their original size (as you can see at the xaml code) I set the width to 200. It is just a display thing, I do not have to store the new image. I would like to do it on the fly, preferably by setting image properties in the xaml. (for all three sizes of circles)
Is using the CroppedBitmap the best approach? http://msdn.microsoft.com/en-us/library/ms752345.aspx it is for windows rt by the way.
One option would be to use a clipping mask:
<Image Source="MyImage.jpg">
<Image.Clip>
<RectangleGeometry Rect="10,10,80,80"></RectangleGeometry>
</Image.Clip>
</Image>
The rect structure takes X,Y, Width and Height values that you have to set depending on your image.

Control for rendering SVG graphics?

I need a control for rendering SVG graphics. The data is coming in the in the form of a StreamReader object. What's the easiest way to render such an image?
At present, I'm using PNGs:
But I'd like something higher resolution. SVGs would be perfect for this.
Sample SVG:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.28.0 (20110507.0327)
-->
<!-- Title: G Pages: 1 -->
<svg width="262pt" height="216pt"
viewBox="0.00 0.00 262.00 216.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph1" class="graph" transform="scale(1 1) rotate(0) translate(4 212)">
<title>G</title>
<polygon fill="white" stroke="white" points="-4,5 -4,-212 259,-212 259,5 -4,5"/>
<!-- a -->
<g id="node1" class="node"><title>a</title>
<polygon fill="purple" stroke="purple" points="159.618,-186.523 133,-198.872 106.382,-186.523 116.549,-166.541 149.451,-166.541 159.618,-186.523"/>
<polygon fill="none" stroke="purple" points="165.003,-188.397 133,-203.245 100.997,-188.397 114.139,-162.57 151.861,-162.57 165.003,-188.397"/>
<polygon fill="none" stroke="purple" points="170.387,-190.272 133,-207.617 95.6126,-190.272 111.729,-158.598 154.271,-158.598 170.387,-190.272"/>
<text text-anchor="middle" x="133" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">a</text>
</g>
<!-- b -->
<g id="node3" class="node"><title>b</title>
<ellipse fill="none" stroke="black" cx="133" cy="-100" rx="27" ry="18"/>
<text text-anchor="middle" x="133" y="-96.3" font-family="Times New Roman,serif" font-size="14.00">b</text>
</g>
<!-- a->b -->
<g id="edge2" class="edge"><title>a->b</title>
<path fill="none" stroke="black" d="M133,-158.413C133,-149.086 133,-138.069 133,-128.192"/>
<polygon fill="black" stroke="black" points="136.5,-128.057 133,-118.057 129.5,-128.057 136.5,-128.057"/>
</g>
<!-- c -->
<g id="node4" class="node"><title>c</title>
<polygon fill="none" stroke="black" points="144.42,-41 22.2639,-41 -0.419833,-5 121.736,-5 144.42,-41"/>
<text text-anchor="middle" x="72" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">hello world</text>
</g>
<!-- b->c -->
<g id="edge3" class="edge"><title>b->c</title>
<path fill="none" stroke="black" d="M120.656,-83.8226C112.588,-73.903 101.855,-60.7069 92.5226,-49.2327"/>
<polygon fill="black" stroke="black" points="95.0581,-46.8031 86.0329,-41.2536 89.6275,-51.22 95.0581,-46.8031"/>
</g>
<!-- d -->
<g id="node6" class="node"><title>d</title>
<polygon fill="none" stroke="black" points="194,-3.55271e-015 225.296,-34.5 162.704,-34.5 194,-3.55271e-015"/>
<text text-anchor="middle" x="194" y="-19.3" font-family="Times New Roman,serif" font-size="14.00">d</text>
</g>
<!-- b->d -->
<g id="edge5" class="edge"><title>b->d</title>
<path fill="none" stroke="black" d="M145.344,-83.8226C154.961,-71.9983 168.365,-55.5183 178.67,-42.8489"/>
<polygon fill="black" stroke="black" points="181.629,-44.757 185.224,-34.7906 176.199,-40.3401 181.629,-44.757"/>
</g>
<!-- e -->
<g id="node7" class="node"><title>e</title>
<polygon fill="none" stroke="black" points="254.137,-199 189.863,-199 208.407,-163 235.593,-163 254.137,-199"/>
<text text-anchor="middle" x="222" y="-177.3" font-family="Times New Roman,serif" font-size="14.00">e</text>
</g>
</g>
</svg>
When I had looked into using SVGs in my WPF applications, I found there were a couple packages that could be added in to provide this functionality, but in the end went with using SVGs that I converted to XAML, which will be able to scale in WPF applications the same way an SVG image is able to scale on browsers and such. If you have access to the SVG code (which it looks like you do), then this may be a good solution for you.
These are the steps I use to achieve this:
Converting SVG to XAML
I prefer to use Inkscape for the following steps.
Open the SVG in a image editor (e.g. Inkscape). The editor must support opening an SVG and saving the SVG as a XAML file.
From the editor, save the file as a XAML. The file should be saved as Silverlight compatible if given the choice. Close the image editor.
If any color changes are desired, open the file in a text editor (e.g. Notepad++, Visual Studio) and find any paths with a color value (look for something like 'fill=“#000000”') and change it to the desired hex color value. Save the file and close the text editor when finished.
Using a XAML image in a WPF project
Right click the resources folder the image should be kept in (e.g. \Resources\Images), pick the option to add an exisitng file.
Make sure the file selection type includes XAML files. Navigate to the file to use and add it.
Open the Properties pane in Visual Studio (right click the file and click the 'Properties' option). Under the 'Advanced' section in the Properties pane, set 'Build Action' to 'Resource' and Copy to Output Directory' to 'Do not copy'.
The image can now be used by setting the source of an item to display the image (e.g. a Frame) to the resource path (e.g. “/resources/images/.xaml”). This can be set either directly or through binding.
Example WPF/XAML code for displaying a XAML image
The following code shows an example of how to display a XAML image added using the above steps. This code will scale the image to fill the container it is added to. The source for the image is supplied through the binding used for the Frame element's source. This can be replaced with a string of the image's path (which is what is be provided through the binding value).
<Viewbox Stretch="Uniform"
Margin="4,4"
VerticalAlignment="Center">
<Frame Source="{Binding ImageSource}"
NavigationUIVisibility="Hidden"/>
</Viewbox>
An example of the path string is:
/resources/images/<file-name>.xaml
If the image is located in a DLL and is used in the same DLL, the path string will need to include more information using the following format:
/<AssemblyName>;component/resources/images/<file-name>.xaml
You can also consider a web render control, like WebView2, to display the SVG file.
Another idea is to render the SVG file to an image, ie. using Svg.Skia, and display the rendered image.

In a WPF user control, how do I specify coordinates relative to the control's total size, so that the control will scale correctly?

I have created this simple expand button user control with WPF:
        
I designed the thing with Width and Height both set to 100 so that I could see what I'm actually doing. The stripped-down XAML of this user control is:
<UserControl x:Class="Foobar.ExpandButton"
...
Width="100" Height="100">
...
<Border>
...
<Canvas>
<Line ... X1="20" Y1="20" X2="50" Y2="50"/>
<Line ... X1="80" Y1="20" X2="50" Y2="50"/>
<Line ... X1="20" Y1="50" X2="50" Y2="80"/>
<Line ... X1="80" Y1="50" X2="50" Y2="80"/>
</Canvas>
</Border>
</UserControl>
Eventually, the button should be able to dispay correctly at various sizes, e.g. at 20 × 20 points. However, due to the coordinates used with the Line elements, I cannot just insert this user control in another window like this:
<foobar:ExpandButton Width="20" Height="20" /> <!-- doesn't scale correctly! -->
I could apply a LayoutTransform → ScaleTransform each time that I use the element at another size than its default 100 × 100 points, but there has to be a better solution.
How can I define the lines' coordinates such that they are relative to the user control's total size?
You can put the image inside a view box, or inside a VisualBrush and use a rectangle to draw it (set the brush to the Rectangle.Fill property).
Even better, convert your image to a Drawing objects (replace Canvas with DrawingGroup and the shapes inside it with GeometryDrawing) and use DrawingBrush to paint it.

Resources