How can I change image.source with C#? - c#

I'm developing a Windows Phone app.I have an image . This is its XAML code:
<Image x:Name="imageclock" Grid.Row="1"
Source="Image/Myimage.png" Height="240" Width="240"
/>
And i want change image.source with this code:
private void ClickonBtn(object sender, EventArgs e)
{
BitmapImage bm = new BitmapImage(new Uri("Image/Darktheme.png", UriKind.RelativeOrAbsolute));
imageclock.Source = bm;
}
But when i complied, imageclock.Source=Null and this is error:
An exception of type 'System.NullReferenceException' occurred in Newappver1.DLL but was not handled in user code

Your code looks ok but maybe you need to add # before the image path to handle the / in the code behind like this :
BitmapImage bm = new BitmapImage(new Uri(#"Image/Darktheme.png", UriKind.RelativeOrAbsolute));

imageclock.Source = new BitmapImage(new Uri("ms-appx:///Image/Darktheme.png"));
source https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

Related

Insert image to Button Foreground

I want to add a Background colour and 9 Foreground Image to 9 buttons from code.
I wish to change the images from C# not in WPF / xaml.
The Background colour works OK using:
button1.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.Red);
Windows forms has an easy solution using:
pictureBox1.Image = Properties.Resources.P1; // this does not work for UWP
What I have tried so far ends up in error messages:
I have changed the Build Action property of P1.png from Content to PRIResource with no success.
string url = "../../Images/P1.png";
//string url = "PW.png";
image1.Source = new BitmapImage(new Uri(url, UriKind.Relative)); //.Uri cannot be converted into a Windows.Foundation.Uri.
//image1.Source = new BitmapImage(new Uri(url, UriKind.Absolute)); //format of url could not be determined
<Button x:Name="button1" Content="Button" Grid.Column="0" Grid.Row="0"
Tag="1" Background="Gray" Padding="0" UseLayoutRounding="False"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="button1_Click">
<Button.Foreground>
<ImageBrush Stretch="Fill" ImageSource="P1.PNG"/>
</Button.Foreground>
</Button>
Solution:
To insert image to Button in C# using VS2015, UWP and Windows 8.1:
Add an Image to the front of your Button. eg Name = button1 and imageX.
Ensure that you set “Build Action” Properties of *.png to Content.
enter code hereImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Images/PW.PNG"));
button1.Background = imageBrush;
BitmapImage bitImage = new BitmapImage();
bitImage.UriSource = new Uri("ms-appx:///Images/PW.PNG");
imageX.Source = bitImage;
Thanks Romasz for your assistance.

how to change resource in wpf by code in c#?

Having the next XAML code
<Window.Resources >
<ImageBrush x:Key="tile" ImageSource="afraid.png"
Opacity=" 20" TileMode="Tile"
ViewportUnits="Absolute"
Viewport=" 0,0,32,32"
></ImageBrush>
</Window.Resources>
I want to change the image by c# code, during the run-time
I have the next code to do this, but the code does not work & I don`t know the reason:
ImageBrush img = ( ImageBrush )this.FindResource( "tile" );
ImageSourceConverter conv = new ImageSourceConverter();
ImageSource src = ( ImageSource )conv.ConvertFromString( "mad.png" );
img.ImageSource = src;
Please refer to this answer.
Try this, create a BitmapImage from a file and set it as the ImageSource of the ImageBrush.
ImageBrush img = (ImageBrush)this.FindResource("tile");
//change the imagesource in runtime,
img.ImageSource = new BitmapImage(new Uri("mad.png", UriKind.Relative));

get the source of image when user clicks it in c#

i have included four photos in xaml code as follows
<Image Grid.Column="0"
Source="Assets/1.png"
Name="m1"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="1"
Source="Assets/2.png"
Name="m2"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="2"
Source="Assets/3.png"
Name="m3"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="3"
Source="Assets/4.png"
Name="m4"
MouseLeftButtonDown="selected"/>
i want to get the source of the image in "selected" function.
my selected function is as follows
private void selected(object sender, MouseButtonEventArgs e)
{
//do somethings....
}
How can i assign the source of the selected image(sender) to a new Image object?.
something similar to follows
Image newimage = new Image();
newimage.Source = //something..
Is there a way to dynamically get the source?
Cast your sender as an image and you will be able to use the Source property:
private void selected(object sender, MouseButtonEventArgs e)
{
Image newimage = new Image();
newimage.Source = ((Image)sender).Source;
}
Use OriginalSource property of event and cast it to Image:
var clickedImage = (Image)e.OriginalSource;
Image newimage = new Image();
newimage.Source = clickedImage.Source;

How to add grid background Image in c#

I am trying to change images source or image background in c# when a button is click with xaml design UI.
<Grid x:Name="gridimage">
<Image x:Name="Image" stretch="Fill"/>
</Grid>
private void Button1_click(object sender, RoutedEventArgs e)
{
// NOT WORKING FOR ME
gridimage.Source = new BitmapImage (new Uri("location"));
gridimage.Background = ?
}
Assumed that there is an Assets folder in your Visual Studio project that contains an image file 2.png, and that the image file's Build Action is set to Resource, you would create a BitmapImage in code behind from a Pack URI like this:
var uri = new Uri("pack://application:,,,/Assets/2.png");
var bitmap = new BitmapImage(uri);
Then you would use the BitmapImage as Source of your Image control:
Image.Source = bitmap;
Or use it with an ImageBrush as Background of your Grid:
gridimage.Background = new ImageBrush(bitmap);

Bind Xaml bitmap image

I have bitmap image variable and i want to bind it to my xaml window.
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string[] resources = thisExe.GetManifestResourceNames();
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SplashDemo.Resources.Untitled-100000.png");
Bitmap image = new Bitmap(stream);
And this is my xaml code
<Image Source="{Binding Source}" HorizontalAlignment="Left" Height="210" Margin="35,10,0,0" VerticalAlignment="Top" Width="335">
</Image>
can u assist me binding this bitmap variable to this xaml image by C# code?
If you really want to set it from C# code and not from inside XAML, you should use this easy solution described further on the MSDN reference:
string path = "Resources/Untitled-100000.png";
BitmapImage bitmap = new BitmapImage(new Uri(path, UriKind.Relative));
image.Source = bitmap;
But first, you need to give your Image a name so you can reference it from c#:
<Image x:Name="image" ... />
No need to reference Windows Forms classes.
If you insist on having the Image embedded into your Assembly, you need the following more lengthy code to load the image:
string path = "SplashDemo.Resources.Untitled-100000.png";
using (Stream fileStream = GetType().Assembly.GetManifestResourceStream(path))
{
PngBitmapDecoder bitmapDecoder = new PngBitmapDecoder(fileStream,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
ImageSource imageSource = bitmapDecoder.Frames[0];
image.Source = imageSource;
}
Here is some sample code:
// Winforms Image we want to get the WPF Image from...
System.Drawing.Image imgWinForms = System.Drawing.Image.FromFile("test.png");
// ImageSource ...
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
// Save to a memory stream...
imgWinForms.Save(ms, ImageFormat.Bmp);
// Rewind the stream...
ms.Seek(0, SeekOrigin.Begin);
// Tell the WPF image to use this stream...
bi.StreamSource = ms;
bi.EndInit();
Click here to view reference
If you are using WPF, right click your image in your project and set the Build Action to Resource. Assuming your image is called MyImage.jpg and is in a Resources folder in your project, you should be able to reference it directly in your xaml without using any C# code. Like this:
<Image Source="/Resources/MyImage.jpg"
HorizontalAlignment="Left"
Height="210"
Margin="35,10,0,0"
VerticalAlignment="Top"
Width="335">
</Image>

Categories

Resources