Loading an image from resources in an unreferenced assembly - image

I have two silverlight assemblies, CaseManager.Applet and CaseManager.Applet.Sample. The Sample assembly has a reference to the base Applet assembly. The sample assembly has an embedded resource png image. The base applet assembly has a view xaml that I wish to display that image programmatically.
In the sample assembly I have a bit of code that creates a Uri like so:
var icon = new AppletIcon()
{
ImageUri = new Uri("CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),
ModuleType = GetType(),
Text = "Sample Module"
};
When I execute this code all the properties of ImageUri throw InvalidOperationException. I am not sure why. Anyone have suggestions?

The following code does the job:
var icon = new AppletIcon()
{
ImageUri = new Uri("/CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),
Module = this,
Text = "Sample Icon"
};
Things to note here:
The slash at the start of the Uri string.
The short name of the assembly containing the resource.
the ;component/ section.
From there it is basically the path inside your project to the image. Hope this helps someone else.
For what it was worth I was missing the very first slash.

Related

.svg Image in MAUI Blazor

i am trying to set an image in to a razor page in MAUI Blazor.
In MAUI (only), there was the aproach, that you have a .svg image in the folder Resources/Images. MAUI then converts the .svg image in a .png image which you can use in the XAML file. like so:
Now i have the same picture in a MAUI Blazor app and i hoped that i can put my picture in the same way expect that i have to use the HTML style like so:
<img src="one_list2.png">
But this doesn't work at all. I tryed with or witout path, path with slashes, backslashes etc. nothing works.
Trying to put a .png image into the wwwroot folder works. But this isn't the goal. I found it very nice to put a svg image which is then converted into a png depending of its size. This way all pictures would be converted exactly in the perfect size you will need lossless.
Thanks
First, Add the image to Resources\Raw and set it to MauiAsset compilation type
Second, Check the project file to avoid setting the image in the other folder.
In razor component HTML:
<img src="#imageSource">
In the code part:
private string? imageSource;
protected override async Task OnInitializedAsync()
{
try
{
using var stream =
await FileSystem.OpenAppPackageFileAsync("testimage.png");
using var reader = new StreamReader(stream);
byte[] result;
using (var streamReader = new MemoryStream())
{
stream.CopyTo(streamReader);
result = streamReader.ToArray();
}
imageSource = Convert.ToBase64String(result);
imageSource = string.Format("data:image/png;base64,{0}", imageSource);
}
catch (Exception ex)
{
//error
}
}
More information you can refer to ASP.NET Core Blazor Hybrid static files.

Open PDF file with page number/index in UWP apps

Is there any option to Open a PDF file that is available in local state folder (inside app installation directory) with the page number. There is one method (launchFileAsync) to open such files but I don't find any option to pass page number/index to open specific page.
Thank you! Any help is appreciated.
UWP provides the PdfDocument class for parsing PDF files, and this class provides the GetPage method for obtaining the object of the corresponding page (PdfPage) through the index.
This is simple code:
PdfDocument pdfDocument;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("xxx.pdf");
pdfDocument = await PdfDocument.LoadFromFileAsync(file);
using (var firstPage = pdfDocument.GetPage(0))
{
var stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
// do something...
}
Here is the complete code example:
PDF document sample

How to dynamically add view in page or layout

I can't figure out how to programatically add a view into a layout or page.
I need to add views at runtime without using static xml declaration since i need to fetch them from an http requested object... . I didn't find useful informations in the docs.
Anyone knows how to do?
I think you meant to dynamically add some view / controls to the page rather than to navigate into another page.
If so, you just need to add some controls into one of the layouts in your page (only containers [=layouts] can have multiple children.
so, your code (viewmodel/page controller) would look something like:
var layout = page.getViewById("Mycontainer");
// create dynamic content
var label = new Label();
label.text = "dynamic";
// connect to live view
layout.addChild(label)
In addition to having a page included inside your app (normal); you download the xml, css, & js to another directory and then navigate to it by then doing something like page.navigate('downloaded/page-name');
you can also do
var factoryFunc = function () {
var label = new labelModule.Label();
label.text = "Hello, world!";
var page = new pagesModule.Page();
page.content = label;
return page;
};
topmost.navigate(factoryFunc);
https://docs.nativescript.org/navigation#navigate-with-factory-function
You should check out this thread on the {N} forum.
The question is about dynamically loading a page and module from a remote server. The (possible) solution is given in this thread.

Titanium ImageView wont display images?

I created an image directory in my projects in the UI folder to place my images.
So the full path is currently Resources/UI/Images.
When i create an image view it wont display the images.
I tried different options, even a web image but nothing works?
var self = Ti.UI.createView({
backgroundColor:'white'
});
var imgv = Titanium.UI.createImageView({url:"http://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Volkswagen_Logo.png/600px-Volkswagen_Logo.png"});
self.add(imgv);
var imgv = Titanium.UI.createImageView({url:"../images/sb02.jpg"});
self.add(imgv);
var imgv = Titanium.UI.createImageView({url:"Resources/ui/images/sb03.jpg"});
self.add(imgv);
There is an error in your code. There is no url property for ImageView control. You should use image property. Try the following code
var imgv = Titanium.UI.createImageView({
image:"../images/sb02.jpg"
});
self.add(imgv);
To answer my question thanks to Anand for the tips:
Images should be placed in the Resources dir
Afterwards reference them just by /folder/image.jpg in my case /images/sb1.jpg

Adding image on dynamically created Button in Silverlight

I am creating the button in silver light dynamically as
HistoryButtton = new Button();
HistoryButtton.Content = "History";
HistoryButtton.Height = GetPercentageValue(TotalHeight, 8);
HistoryButtton.Width = GetPercentageValue(TotalHeight, 15);
I tried this solution
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(#"Images/history.png", UriKind.Relative));
HistoryButtton.Background = brush;
But its not working.
I also tried this solution
Uri uri = new Uri("/Images/history.png", UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uri);
Image image = new Image();
image.Source = imgSource;
HistoryButtton.Content = image;
But this solution also not working. Please help me.Thanks in advance.
try to change your Uri first parameter to be like this
"/SolutionName;component/Images/history.png"
replace SolutionName with your Solution name.
The question and one of the other answers here helped me with the same problem, but I still struggled with this for awhile (actually, more than awhile) because of some really simple things that I missed. Here is the complete code that worked for me:
Dim b As New Button
Dim uri As New Uri("/<project name>;component/<path to image>", UriKind.Relative)
Dim imgSource As New Imaging.BitmapImage(uri)
Dim image As New Image()
image.Source = imgSource
b.Content = image
Two things that kept me from figuring this out sooner:
<project name> is the name of the project where the image resides, which may not be the same project the code resides in.
"component/" is a literal string (and is not a folder in the project). My image file's "Build Action" is set to "Resource".
So if your project name is "MyProject", your image name is "MyImage.png", and the image resides in the folder "MyFolder" in "MyProject", the correct string to pass to the Uri constructor is:
"/MyProject;component/MyFolder/MyImage.png"

Resources