WPF C# Image Source - c#

In XAML, I learned to specify image source with below code:
<Image Source="/WaivePadBankAnim;component/Images/Consumer/search_button_full.png" />
How would I do it in C# .cs?

Try this
Image image = new Image();
image.UriSource = new Uri("/WaivePadBankAnim;component/Images/Consumer/search_button_full.png")
Try this
Adding WPF Controls Progrrammatically

M working on Windows phone 8 and i used this.
OrderImage1.Source = new BitmapImage(new Uri("///Assets/images/order_bread.png"));

Image img = new Image();
img.Source = new Uri("<your path here>");
I hope this helps.

please check this
just use a class "ImageSourceConverter"
img1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("/Assets/check.png");

Related

Setting the source of WPF Image control behind code

Recently I've been strugling with setting the Setting the source of WPF Image control inside code.
I've tried something like:
Image.Source = new BitmapImage(new Uri(#"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute));
But it doesn't seem to work and thorws Object reference not set to an instance of an object exception. Here's the screenshot of the path to my image inside hierachy:
You can use..
var imageSource = new Uri(#"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;
Also, make sure that BlueFolder.png is set as a Resource. Right click the image, and select Properties to do this.

Resource image can be referenced in Xaml but not in code, why?

Excuse the novice question, but I am just learning WPF.
I have an image that I've set the build action to be "Resource"
If I set it as the Window Icon in the XAML it works just fine.
like this:
Window ... Icon="Images/MyIco.png"
But, if I try to set it as the source for an Image, I get an exception the resource is not found:
Uri uri = new Uri("pack://application:,,,/Images/MyIco.png"); // This does not work
img.Source = BitmapFrame.Create(uri);
What am I doing wrong in the code above?
Make sure your path is correct, if true, u can try this code to load your image
//tmp is your path
BitmapImage img = new BitmapImage(new Uri(tmp, UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += img_ImageOpened;
void img_ImageOpened(object sender, RoutedEventArgs e)
{
WriteableBitmap wbm = new WriteableBitmap((BitmapImage)sender);
}
Ex: my path is "/Assets/sizes/background/bg_02.png"

Adding newly created image object to window

Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri(#"C:\Users\User\Desktop\Awesomeness\PicProgram\Program\bin\Debug\flower.png", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
I was wondering how I get myImage3 added to the window.
I've tried this.content.add.myImage3 or this.add(myImage3) and other variations none seem to work.
Any suggestions?
Thanks
Seems like a basic wpf usage question, although doing something that is simple in xaml can get a bit complex in code behind.....
You have to add your image to some panel or content control in your window, not the window itself. So if there is a grid in your window, give the grid a name like m_grid, then do m_grid.children.add(myImage3).

How to programmatically set the Image source

When the Image's Source property is set the following way, the picture is taken from /Images/down.png.
How would I do the same thing programmatically?
<Image x:Name="myImg" Source="/MyProject;component/Images/down.png" />
The following would not work, since Image.Source property is not of a string type.
myImg.Source = "/MyProject;component/Images/down.png";
Try this:
BitmapImage image = new BitmapImage(new Uri("/MyProject;component/Images/down.png", UriKind.Relative));
myImg.Source = new BitmapImage(new Uri(#"component/Images/down.png", UriKind.RelativeOrAbsolute));
Don't forget to set Build Action to "Content", and Copy to output directory to "Always".
Try to assign the image that way instead:
imgFavorito.Source = new BitmapImage(new Uri(base.BaseUri, #"/Assets/favorited.png"));
{yourImageName.Source = new BitmapImage(new Uri("ms-appx:///Assets/LOGO.png"));}
LOGO refers to your image
Hoping to help anyone. :)
Use asp:image
<asp:Image id="Image1" runat="server"
AlternateText="Image text"
ImageAlign="left"
ImageUrl="images/image1.jpg"/>
and codebehind to change image url
Image1.ImageUrl = "/MyProject;component/Images/down.png";
try this
PictureBox picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(100, 50),
Location = new Point(14, 17),
Image = Image.FromFile(#"c:\Images\test.jpg"),
SizeMode = PictureBoxSizeMode.CenterImage
};
p.Controls.Add(picture);

Change button background

After several attempts to change the background of a button, and getting errors of casting and things like that I finnaly got to this point:
Uri dir = new Uri("red_flag.png",UriKind.RelativeOrAbsolute);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
ImageBrush bru = new ImageBrush();
bru.ImageSource=source;
bru.Opacity = 100;
This code does not generate errors, but I can't see the changes when I call:
button1.background = bru;
It just makes anything! :(
found the answer myelf after reading Mick's answer, I share with you what I did:
Uri dir = new Uri("red_flag.png", UriKind.Relative);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
Image image = new Image();
image.Source = source;
StackPanel stack = new StackPanel();
stack.Children.Add(image);
myButton.Content = stack;
Thanks for your help
Update 1:
For best results set the padding property of your button to 0 (in each of the cases) so the image can resize automatically to fill all the button, please note this could hide your actual content, in my case this was what I wanted.
If this code is part of the click event handler for the same button you will have this problem.
Peter Torr explains why here and offers a solution.
Why can't I change the Background of my Button on a Click event?

Categories