Drawing an SVG File in WinRT - c#

I'm trying to draw a SVG File. So far I've tried using an Image tag and a WebView with the svg itself or an object / embed tag. Nothing so far seems to help, and all the dll's I've found so far need .NET.
Examples of what I've tried so far (note: Paths have all been tried with or without ms-appx:///, note2: using img tag in webview for non-svg types hasn't worked either)
WebView:
webView.NavigateToString("<embed src='circle1.svg' type='image/svg+xml' />");
webView.NavigateToString("<object data='circle1.svg' type='image/svg+xml' />");
webView.NavigateToString("<svg>....</svg>");
Image:
<Image Source='circle1.svg'>

I am not sure if you can include external images this way, have you tried with other image formats? Have you tried using an absolute path? I would also consider using the data tag as image source instead of an external file.
Example (from wikipedia):
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
alt="Red dot">
Additionally, there is project in codeplex called xamltune that can convert a svg file into an xaml file. Maybe you can insert the output xaml code in your application instead of using a WebView.

Related

Using an SVG as a Ribbon SmallImageSource, with SharpVectors?

I've successfully been able to use a static .svg file as an image in WPF by following the guidance in another question.
The basic approach there is to use the SharpVectors library, and do:
<svgc:SvgViewbox Source="path/to/file.svg"/>
In place of an <Image .../> tag.
However I am struggling trying to find a similar method to use an SVG within a System.Windows.Controls.Ribbon - where i'd like to use it as the SmallImageSource of a RibbonMenuButton.
I have tried the following:
<RibbonMenuButton Label="Test">
<RibbonMenuButton.SmallImageSource>
<svgc:SvgViewbox Source="path/to/file.svg"/>
</RibbonMenuButton.SmallImageSource>
</RibbonMenuButton>
Which produces the compiler error message:
The specified value cannot be assigned. The following type was
expected: "ImageSource".
I think the key problem is that an svgc:SvgViewBox is not an "image source", but I don't know how to properly convert or otherwise work around this.
I'm open to alternate approaches which don't use SharpVectors, but it is extremely convenient to have source image files in SVG format and not have to manually convert to any other format.
SharpVectors includes a converter extension which can be used to 'output' an ImageSource.
This is documented in section "1.2 WPF Extensions and Type Converters" of their usage guidelines.
Example:
<RibbonMenuButton Label="Test" LargeImageSource="{svgc:SvgImage path/to/file.svg}"/>
(where svgc is the defined name of the SharpVectors namespace in your XAML.)
The svgc:SvgImage binding extension produces a DrawingImage which is a type of ImageSource. This works perfectly at runtime with the SVG image rendered into the button.
Unfortunately at design-time the button image is blank.
Please try using the newly added property; AppName, which is used to try and resolve the URI of the resource file at design-time.
See the samples for the SvgImage and newly added SvgImageConverter, especially the toolbar demo using the SVG icons.
https://github.com/ElinamLLC/SharpVectors/tree/master/TutorialSamples/ControlSamplesWpf
SvgImageConverter provides binding support, if you need it unlike the SvgImage.

img tag doesnt print thumbnailed photo

im doing a project using .net MVC web Application.
i recently used a code to create a new thumbnail to photos (re defining the sizes of a photo) . and now i'm trying to print the photos using the 'img' tag of HTML but without success.
the thumbnail creating code :
Image thumbnail = Image.FromFile(destPath);
thumbnail = (Image)(new Bitmap(thumbnail, new Size(this.m_thumbnailSize, this.m_thumbnailSize)));
thumbnail.Save(destPathThumb);
now for the img printing :
#Html.DisplayFor(modelitem=>item)
<img class="img-rounded" style="border-radius:50%" src=#item alt="">
item is the path to the picture and it is currect (ive checked serveral times).
any ideas what i could be ?
thank you for helping me ! :)
EDIT: the img tag prints normal pictures -> pictures that my program did not create(as i said,my program created thumbnail pictures)
I wonder if it is not releasing the file lock on the image, I have had that problem in the past. You have to be very careful about releasing those Image and Bitmap objects.
Something like this might work, as it will dispose the image before the page starts using it:
using(Image thumbnailoriginal = Image.FromFile(destPath)){
using(Image thumbnail = (Image)(new Bitmap(thumbnailoriginal, new Size(this.m_thumbnailSize, this.m_thumbnailSize)))){
thumbnail.Save(destPathThumb);
}
}
It seems like a lot of brackets on the end but thats ok.
If it still does not work, can you post the html that is sent to the browser, you can do that using "View page source" from the right-click menu in Chrome.
Now we have the html that is going to the browser, we can see that the src attribute is pulling directly from the C drive, rather than from a web url:
<img class="img-rounded" style="border-radius:50%" alt="" src="C:\Users\Operu\Desktop\dest\Thumbnails\2018\6\Sasuke.png">
Normally we would want to see something more like this:
<img class="img-rounded" style="border-radius:50%" alt="" src="\Thumbnails\Sasuke.png">
Could you post the model and controller files next please, so we can see what type of object the #item is when it hits your view. Then we can hopefully get it to pull the relative url address instead of the file location.

Adding src Dynamically to img in C#

I have an img tag.
(I am using just image and not asp.net because it is an imageMap that I have working with an img).
I dynamically set the src as below in 3 different ways. The first way works and the image is included in the project. But when I try to add the src the 2nd way it fails to display the image. That image is not part of the project. I have even tried the 3rd way for a different location but it fails to load as well. It is not part of the project either. What I need to be able to do is load ans image src dynamically that is not included in the project and have it display.
<img runat="server" class="ImageMap" id="ImageMapID" src="" usemap="#imagemap" />
ImageMapID.Attributes["src"] = #"/Icons/XXXX XXXX.png";//first way that works
ImageMapID.Attributes["src"] = #"/documents/XXXXX/XXXX_04092017.pdf"; // second way that fails
ImageMapID.Attributes["src"] = #"file://C:/temp/XXXXXXXX.pdf";// third way that fails
Any ideas anybody? I should add they are different images but they should all work.
First thing you should know, PDF files are not image objects, so you can't use img tag to embed it. You need to use iframe tag instead:
<iframe src="file:///C:/temp/XXXXXXXX.pdf" style="width: 100%; height: 100%;" frameborder="0" scrolling="no">
...
</iframe>
Or use embed/object tag with same file path:
<embed src="file:///C:/temp/XXXXXXXX.pdf" width="600" height="400" type="application/pdf">
<object type="application/pdf" data="file:///C:/temp/XXXXXXXX.pdf" title="XXXXX" width="600" height="400" >...</object>
If you want to include local file path for images, use file reference path or relative path with proper directory structure shown in example below:
ImageMapID.Attributes["src"] = #"file:///C:/temp/Icons/XXXXXXXX.png";
// watch out for current relative path you're using
ImageMapID.Attributes["src"] = #"../../temp/Icons/XXXXXXXX.png";
Note that Windows local drive path uses this format to refer any files for use in any HTML elements with source attribute:
file:///[drive letter]:/[path_to_resource]/[filename].[extension]
References:
How to properly reference local resources in HTML?
Recommended way to embed PDF in HTML?

Umbraco GetCropUrl not giving me a valid URL with Image Cropper

I'm using the image cropper on my media type > Image > Upload Image. This seems to be the best solution for creating responsive images in the media library.
However, this presents me with the problem of finding out how to get the URL for my images now.
Usually for Image Croppers I would use this code:
#Model.Content.GetCropUrl("image", "my-crop-name")
However if I try to get the image crop this way I get this instead:
<img src="Umbraco.Web.Models.DynamicPublishedContent?mode=pad&rnd=131108182860000000" />
I was expecting to get an image URL with the crop I specified. This works fine for standard image croppers but not ones on my images in the media library. Why is that? And how should I get the crop URL for these images?
I'm using v7.4.2
In order to get a crop URL for an image in the media section you first need to retrieve the image as IPublishedContent. You can do this as follows:
var imageId = Model.Content.GetPropertyValue<int>("image");
var image = Umbraco.TypedMedia(imageId);
Or if you're using the excellent Core Property Value Converters package you can retrieve it directly without having to perform the conversion:
var image = Model.Content.GetPropertyValue<IPublishedContent>("image");
You then need to call the GetCropUrl() method on your image. If you've replaced the default Upload property with an Image Cropper property, and kept the property alias the same (umbracoFile), you can just pass in the crop alias:
var cropUrl = image.GetCropUrl("my-crop-name");
If the alias of your Image Cropper property is different to the default you will need to pass that as an additional argument:
var cropUrl = image.GetCropUrl("imageCropperAlias", "my-crop-name");
For images directly from the media libary without an Image Cropper data type. I use somethings like this:
<img src="#photo.Url?mode=crop&width=634&height=634" alt="#photo.Name" />
Not very pretty but it works perfectly.

taking a screenshot of some element in the html

So I was wondering, if I have a div which contains a maps of google maps rendered with some makers and I want to take a picture of that element how can I do this?.I found this solutions it's a approach of what I need but the problem it's that sending a url doesn't help me, I need the specific Div with all the markers. For the server side i'm using C# and for the cliente, asp.net with jquery. Also the google maps apiV3 to handle the maps
Take a look at the Canvas2Image library by Nihilogic Labs:
http://www.nihilogic.dk/labs/canvas2image/
Here's an example of how to take a screenshot of an element through JavaScript:
var oCanvas = document.getElementById("thecanvas");
Canvas2Image.saveAsPNG(oCanvas);
EDIT
You might also take a look at Html2Canvas and FlashCanvas too. I believe one of them has support for earlier browsers (those which don't support HTML5).
Use the method in the link you mentioned and crop the resulting image to the dimensions of your div. You can use myDiv.getBoundingClientRect() in JavaScript to get the dimensions of your div.
Using the WebBrowser control, you can manipulate the document using html or JavaScript to get the page in the state you need to take your screenshot.
Have you considered using the Google Maps Static API - this will render a map image, with markers on it, as a PNG.

Categories