WARNING There are two different ImageSources in Windows API
System.Windows.Media.ImageSource = The one of Windows Forms
Windows.Ui.Xaml.Media.ImageSource = The one of Windows Store Apps
I'm a beginner in C# and Windows 8 Metro Style App programming stuff.
But if you want to edit the image of a Image, the "old" method with BitmapImage won't work:
XAML:
<Image Source="http://image.source.de" x:Name="Image1" />
Code-Behind C#:
Image1.Source = new BitmapImage...
won't work. The compiler will say something like
"System.Windows.Media.Imaging.BitmapImage" can't be converted to "Windows.Ui.Xaml.Media.ImageSource"
(Yes, BitmapImage can usually be converted to System.Windows.Media.ImageSource)
(System.Windows.Media.Imaging isn't anymore in the default import list of Visual Studio for Windows (StoreApps), you need to bind PresentationCore.dll first.)
So - is there any solution to edit Image1.Source than using Bindings?
This should work.
Image.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new System.Uri("http://image.source.de"))
Related
I'm looking for a method to make all my resource files work properly after I have written my project in a cd.
For example I have saved my images in Project Name\Project Name\obj\Debug\Images and I want them to be usable both in xaml and regular c# code, when I insert that cd in another machine.
Thanks for your time!
What worked for me was, using the Image class for both backgrounds and overlapping images.
In xaml the code is <Image x:Name="Image1" Source="..\obj\Debug\Images\image.jpg"/>
and you initialise it in c# using
public PageName()
{
InitializeComponent();
Image1.Source = new BitmapImage(new Uri(#"../Images/cafeteria.jpg", UriKind.Relative));
}
It's a shame because (atleast in my case) the ImageBrush Class didn't support relative uris for some reason..
I'm currently evaluating Xamarin Forms as an alternative to our webbased HTML applications targeting mobile platforms.
Our applications often use graphical symbols embedded in paragraphs of text.
The desired effect looks like this:
Of course the text also has to be able to freely wrap around, including all the symbols. In HTML this is simply achieved like this:
<p>Sample text with <img src="sample.jpg"> embedded</p>
How can I achieve the same effect using Xamarin Forms? I already looked at FormattedStrings which allow formatting of subparagraphs of Labels, however they do not seem to allow embedding of images.
Also please note that the solution is required to support iOS, Android and Windows Phone 8.1 at least.
Obviously being forced to use a WebView almost defeats the point of moving a HTML5 app to Xamarin!
In the Xamarin Forums a similar question has been asked: https://forums.xamarin.com/discussion/1649/text-with-image
To solve the problem Tomasz Cielecki cooked up some sample code here: https://gist.github.com/Cheesebaron/5034440
He then went onto blog about it here:
http://blog.ostebaronen.dk/2013/02/adding-images-to-textview-and-edittext.html
<snip>
I started out with a super simple sample trying to get an Image shown in a TextView. I googled up some solutions and sure, Spannables allow using ImageSpan inside of them! There were nice samples and such, and I came up with this.
ImageSpan in TextView
//Load up your drawable.
var imageSpan = new ImageSpan(this, Resource.Drawable.Icon);
//Set the text of SpannableString from TextView
var spannableString = new SpannableString(textView.Text);
//Add image at end of string
spannableString.SetSpan(imageSpan, textView.Text.Length-1, textView.Text.Length, 0);
Easy, huh? And you can add loads of other Spans to the Spannable, such as StyleSpan, which you can style your fonts with bold, italic and other styles.
Since that was so easy, I quickly tried to do that with an EditText. It also works just fine...
</snip>
Currently, the only way to have symbols and images within text blocks is to use WebView (https://developer.xamarin.com/guides/xamarin-forms/working-with/webview/)
var browser = new WebView();
var htmlSource = new HtmlWebViewSource ();
htmlSource.Html = #"<html><body>
<h1>Xamarin.Forms</h1>
<p>Welcome to WebView.</p>
</body></html>";
browser.Source = htmlSource;
I have been searching for a solution, but I couldn't succeed. The problem is as follows.
I have a WPF application where I am using FlowDocuments. I want to create a corresponding application on Android and share documents between those applications (WPF and Android) using a web service.
Android has SpannableString. But I couldn't figure out if we have a control in Android, like RichTextBox in WPF.
To sum up my question, Is there a format like Rich Text Format and an Android control to both display and manipulate this document on Android?
You can make use of the TextView together with the span fields to set different text styles (similar to Runs on XAML). There is also a SpannableStringBuilder which might ease this process.
SpannableString text = new SpannableString("Hello World!")
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, 0);
text.setSpan(new StrikethroughSpan(), 7, 11, 0);
This question already has answers here:
Change WPF window background image in C# code
(7 answers)
Closed 7 years ago.
I'd like to change the background image of my WPF application by a button. I know how to do it using WindowsForms, but in WPF I failed.
I found a solution already, but that solution would copy my background images to the output folder next to the Application.exe
This is not really a solution that I desire. I would like to have the images stored inside my application.
Can somebody explain me detailed what I need to do [how to add the images to the program, especially the resource-properties, how to access them in C#....]. It seems like I am too stupid to set it up correctly :P
Thanks in advance
Firstly, add a Folder to your Solution (Right click -> Add -> Folder), name it something like "Resources" or something useful.
Then, simply add your desired image to the folder (Right click on folder -> Add -> Existing item).
Once it's been added, if you click on the image and open the Properties window (Alt+Enter), you'll see the Build Action is set to Resource and the Copy to Output Directory is set to Do not copy.
You can reference the image in C# using the following code:
this.Background = new BitmapImage(new Uri(#"pack://application:,,,/YourApp;component/YourFolder/YourImage.png"));
Or in XAML:
<Image Source="pack://application:,,,/YourApp;component/YourFolder/YourImage.png" ... />
Or:
<Image Source="/YourApp;component/YourFolder/YourImage.png" ... />
Try this:
this.Background = new ImageBrush(new BitmapImage(new Uri(#"pack://application:,,,/Yourapp;component/yourimage.png")));
I want to access some images in my Windows Phone class library, but how can I access these images?
I have a method which will set the image, but
new Uri("/Assets/Images/MyImage.png", UriKind.Relative);
doesn't work. I've tried setting the image to Resource instead of Content, but it also doesn't work.
So how can I provice images in my class library?
From what I gather it looks like you are trying to set the source of an image from the XAML code behind file.
If so you can do so as follows:
MyImage.Source = new BitmapImage(new Uri("/Assets/Images/MyImage.png", UriKind.Relative));
Hope this helps.