Adding image on dynamically created Button in Silverlight - visual-studio-2010

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"

Related

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

Bind Images To RadMosaicTile

I am using RadMosaicTile in my windows store application. I want to bind images with it.
I tried this
RadMosaicHubTile tile = new RadMosaicHubTile();
tile.ImageSources.Add("Image1.jpg");
tile.ImageSources.Add("Image2.jpg");
but it is not working. images are not binded.
Can anyone help me?
Thanks.
The have this code sample on the telerik forum - does this work for you?
Collection<object> imageSources = new Collection<object>();
ImageSource image = GetImageFromWeb();
imageSources.Add(image);
RadMosaicHubTile hubTile = new RadMosaicHubTile();
hubTile.CreateImageSource = (source) =>
{
return (ImageSource)source;
};
hubTile.ImageSources = imageSources;

Loading a local html in WebBrowser

I am trying to load a local html file into a WebBrowser control. However, the resources that come with that html are added to the project but not loaded in the html. I copied them in the same directory, marked them as "copy-always" resources.
Since I am new to windows phone development, any help will be greatly appreciated. Thanks.
Mark the build action for file as 'Content' from 'Properties' and then try the following :
(Assuming you have b.html file in 'html' folder of your project.)
var rs = Application.GetResourceStream(new Uri("html/b.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowserCtrl.NavigateToString(htmlText) ;
(Assuming you have Help.html file in 'Help' folder of your project.)
if you're Mark the build action of the file as 'Content'
var rs = Application.GetResourceStream(new Uri("/Help/Help.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowser1.NavigateToString(htmlText);
if you're Mark the build action of the file as 'Resource'
var rs = Application.GetResourceStream(new Uri("/YOUR_PROJECT_NAME;component/Help/Help.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowser1.NavigateToString(htmlText);
You should mark the HTML File as Content. It should be "Resource" right now.

importing image on canvas html5

var img = new Image();
img.src = '/images/backdrop.jpg';
ctx.drawImage(img,0,0);
I wanted to load an image from local disk on to canvas using dialog box mechanism rather than the path directly specified as in above example. I tried different sorts using JavaScript but in vain, even tried using the input type as file. What else can I try?
Take a look here:
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images
It's important to have drawImage call after the image has loaded:
var img = new Image();
img.onload = function() {
var ctx = document.getElementById('ctx').getContext('2d');
ctx.drawImage(img, 0, 0);
}
img.src = 'images/backdrop.jpg';
Also, note that you probably want to use images/backdrop.jpg instead of /images/backdrop.jpg (note there's no slash in front), as using the latter would get the image from root directory, I would assume that's probably not where your images are.
As far as loading from a dialog box, you can replace the last line from the above with something like this:
var name = prompt("Enter the name of the file", "backdrop.jpg");
img.src = 'images/' + name;
This way, the user would be able to enter the name of the image file to load it. Of course, you need to have that file in your images folder.
Hope this helps.

Loading an image from resources in an unreferenced assembly

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.

Resources