I am trying to load an image into an Image control in a WPF application.
I need to set the Input in one function, and bind it to the Image in another.
This works:
var b = new Binding { Source = (ImageSource)new ImageSourceConverter().ConvertFromString("D:/data/TestPattern.jpg") };
CameraFrame.SetBinding(Image.SourceProperty, b);
But this does not:
BitmapImage bSource = new BitmapImage(new Uri("D:/data/TestPattern.jpg"));
var b = new Binding { Source = (ImageSource)new ImageSourceConverter().ConvertFrom(bSource) };
CameraFrame.SetBinding(Image.SourceProperty, b);
Why is this? Am I missing something in the ConvertFrom function?
Why don't you simply set the Source property to a BitmapImage?
CameraFrame.Source = new BitmapImage(new Uri("D:/data/TestPattern.jpg"));
Related
In my WPF I want images on the screen to change, every time when the user clicks right button. The problem is that I have all the time same error message:
'Invalid URI: The format of the URI could not be determined.'
This is the code:
string pic1 = #"C:/Users/Milk/Desktop/exercises/wpf_1/portraits/1.png";
string pic2 = #"C:/Users/Milk/Desktop/exercises/wpf_1/portraits/2.png";
private void buttonRight_Click(object sender, RoutedEventArgs e)
{
List<string> portraits = new List<string>();
portraits.Add(pic1);
portraits.Add(pic2);
string ShowPicture = portraits[counter % portraits.Count];
image.Source = new BitmapImage(new Uri(portraits.ToString()));
counter++;
}
When I tried just with one string, like this:
image.Source = new BitmapImage(new Uri(pic1));
it was working fine, but once it's in the list, it cannot find the file path - at least, that's how this looks like for me.
Any idea how to fix this and where I am making an error?
It is because .ToString() usually returns namespace of an object (unless overriden), which in this case is List's namespace; you need to pass in actual list values one by one into Uri constructor.
What you need to do is to pass in actual path as such:
string ShowPicture = portraits[counter % portraits.Count];
image.Source = new BitmapImage(new Uri(ShowPicture));
Hi you are linking it to the list Object and not the element inside the list
This should solve your issue :
image.Source = new BitmapImage(new Uri(portraits[0].ToString()));
that will get the pic1
and if you want to get the pic2
you will need to write :
image.Source = new BitmapImage(new Uri(portraits[1].ToString()));
if you want to get both of pic you need to add a loop
Something like :
for (int i=0 ; i < portraits.count ; i++)
image.Source = new BitmapImage(new Uri(portraits[i].ToString()));
//..Do the rest
portraits , let me know exactly your expected result and I will add further details
I'm new in this field. I'm working in windows application, I create panel and adding the buttons inside the panel. i want to retrieve the image from database and set to the background image in button.
This is my code,
FileName = objDR["Photopath"].ToString();
byte[] data = Encoding.UTF8.GetBytes(FileName);
MemoryStream ms = new MemoryStream(data);
image = new System.Drawing.Bitmap(ms);
Buttons[i].BackgroundImage = image;
initially put BackgroundImage property ON
OR
You can Code :
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Image));
How to add a parameter for an image to this variable?
var selectionDisplay = new SelectionDisplay(button.Label as string);
Thanks in advance!
EDIT:
I have this set of images and their respective code below (see pic1)
This is where the program gets the images to be displayed. The code for the buttons is this one:
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(file, UriKind.Relative);
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi)
};
this.wrapPanel.Children.Add(button);
}
This is where the program gets the images to be displayed.
The code for the buttons is this one:
private void KinectTileButtonclick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.fake_fake_fakeource;
var selectionDisplay = new SelectionDisplay(button.Label as string);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
Right now, when i click on of the images, the SelectionDisplay window pops up, which look like this (see pic2). What i want is that when I click an image the SelectionDisplay window should open with the respective image... meaning that if I click on the image with a dog, the window should open with the dog's image, not with other image.
I hope I've made myself clear and that you can help me.
http://i58.tinypic.com/8zl6h3.jpg
http://i57.tinypic.com/208fosy.png
is this the constructor you are talking about? is this where i should make changes? should i add something after "string itemid"?
public SelectionDisplay(string itemId)
{
this.InitializeComponent();
this.messageTextBlock.Text = string.Format(CultureInfo.CurrentCulture,Properties.Resources.SelectedMessage,itemId);
}
I see two approaches:
Just pass the image brush in your constructor. Its view->view, so you aren't breaking MVVM (and it looks like you aren't using that pattern anyways).
new SelectionDisplay(button.Label, button.Background);
Set the path as the "tag" of the button. The tag property is an object you can put whatever you want into (the framework does not use it, and so it is included for this very purpose). Then just pass the string to SelectionDisplay, and instantiate the image just like you are doing for the button:
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = file
};
var selectionDisplay = new SelectionDisplay(button.Label as string, button.Tag as string);
FrameworkElement.Tag on MSDN (Note that Button derives from FrameworkElement, as do all controls, so it automatically has it as well!)
UPDATE
I see that SelectionDisplay is a UserControl in your project, so you just need to change its constructor to look like:
Numbers match above:
SelectionDisplay(string labelText, ImageBrush sourceImage)
SelectionDisplay(string labelText, string imagePath)
That is the source of the error you are getting, you have to modify the constructor to take the new parameter.
Please let me know if I can clarify anything.
Background of the panorama should change depending on FlowDirection of the current language.
So I thought I should do this programmatically (is it possible in the XAML?)
I added this to the OnNavigatedTo event handler of the page:
ImageBrush ib = new ImageBrush();
if (AppResources.ResourceFlowDirection == "RightToLeft")
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackgroundMirror.png"));
PanoramaRoot.Background = ib;
}
else
{
ib.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/CustomBackground.png"));
PanoramaRoot.Background = ib;
}
but after running, there is no image on the background, it is black.
I have a doubt that I am referring to the image badly.
How can I solve this problem?
or if the way is right, how can I get sure that the brush has an image?
Update: I have tested this too, but no difference:
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative));
Your updated code,
ib.ImageSource = new BitmapImage(new Uri("/Assets/CustomBackground.png", UriKind.Relative);
works properly.
You just have to make sure that the Build Action property of the image is set to Content.
I want to insert asp image control inside datalist Itemtemplate through C#. Here is the code below i am currently trying but it didn't work.
DataList DataListCampaign = new DataList();
DataListCampaign.RepeatLayout = RepeatLayout.Flow ;
DataListCampaign.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
DataListCampaign.DataSource = CampTabImage;
DataListCampaign.DataBind();
Image TabImages = new Image();
TabImages.ID = "TabImages";
DataListCampaign.Controls.Add(TabImages);
TabContainer1.Tabs[k].Controls.Add(DataListCampaign);
Where is the problem??
try with this
Image TabImages = new Image();
TabImages.ID = "TabImages";
TabImages.ImageUrl = "~/imagepath";
DataListCampaign.Items[0].Controls.Add(TabImages);