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);
Related
I'm trying to make an UWP application in which you can click an add button, pick a file (an image) with file open picker and then show it in a list view item.
The problem is that I created a 'global' bitmap image as the image source which can be changed
BitmapImage tileBitmapImage = new BitmapImage();
So that its source is later changed to the chosen image on the click event
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{ await tileBitmapImage.SetSourceAsync(fileStream); }
And then the BitmapImage with its source is assigned as an Image source while creating the list view item
Image tileImage = new Image()
{
Source = tileBitmapImage,
Margin = new Thickness(0, 10, 0, 0),
Width = 400,
Height = 100,
};
But every time the user adds another list view item and chooses another image all the previously set images change to the just chosen image
Is there any solution for this?
you should try to create new image object instance per call
like
BitmapImage bmp = new BitmapImage();
IRandomAccessStream stream = await pic.OpenReadAsync();
bmp.SetSource(stream);
Image tileImage = new Image()
{
Source = bmp,
}
having one global tileBitmapImage variable causes all list items to show the same image (the last one was selected by user)
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.
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"
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).
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");