In my WPF application I added few images to my project in Visual Studio and changed their Build Action. And they work.
The problem is when I want to add new images to images folder. After that I launch my .exe file and application cannot see these new added images.
How to resolve this?
private void ShowImage(Button button)
{
string buttonName = button.Name;
string path = $#"Images/{buttonName}.png";
Image image = new Image();
image.Source = new BitmapImage(new Uri(path, UriKind.Relative));
button.Content = image;
}
If I understand your question, I think the answer you are looking for is a component reference that'd look like so:
private void ShowImage(Button button)
{
string buttonName = button.Name;
string path = "/MyProject;component/Images/{buttonName}.png";
Image image = new Image();
image.Source = new BitmapImage(new Uri(path, UriKind.Relative));
button.Content = image;
}
Related
I'm currently trying to update a program by having a button (nextImg) that when clicked calls a function that creates two bitmaps, generates a number starting from 0, and increments every time the nextImg button gets clicked. Then the program takes that number(num) and returns a string from a List[num] which has all the Uri sources of my photos. However when I click next image I get a
System.NullReferenceException.
private void Image_Loaded(object sender, RoutedEventArgs e)
{
// Create a new BitmapImage.
BitmapImage b = new BitmapImage();
BitmapImage b1 = new BitmapImage();
Image img = new Image();
string sSrc = numGen();
b.BeginInit();
b.UriSource = new Uri(sSrc);
b.EndInit();
b1.BeginInit();
b1.UriSource = b.UriSource;
b1.EndInit();
img = sender as Image;
img.Source = b; // Error on this line
Canvas1.Height = Picturebox1.Height;
Canvas1.Width = Picturebox1.Width;
Canvas1.Margin = Picturebox1.Margin;
}
I want to know the image name whenever image clicked.
i used this code for adding image to button background:
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Background = brush1;
now i want to know the name of the image whenever image clicked. please any one help me out.
You can save the name in the buttons Tag property
ImageBrush brush1 = new ImageBrush();
brush1.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/emptyseat.jpg"));
btn.Tag = "emptyseat.jpg";
btn.Background = brush1;
public void OnClick(object sender, RoutedEventArgs e)
{
var btn = (Button)sender;
string name = btn.Tag;
}
In a Windows Phone 8 app, I'm using the following code to display an image:
InitializeComponent();
Image i = new Image();
i.Source = new BitmapImage(new Uri("C:\\Data\\Users\\Public\\Pictures\\Sample Pictures\\sample_photo_05.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
But when the page loads, the screen is empty. Can anyone see what I'm doing wrong?
Copy your image in Asset folder and set Build Action==Content
Image i = new Image();
i.Source = new BitmapImage(new Uri("/yourProjectName;component/Assets/YourImageName", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
Follow below steps to set image by URI in windows phone
1. Copy Image in Images folder in your solution.
2. Set Image as Resource Rightclick on Image->Properties->Build Action ==Content
InitializeComponent();
Image i = new Image();
i.Height =100;
i.Width=100;
i.Source = new BitmapImage(new Uri("/Images/YourImageName", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(i);
Using a CameraCaptureTask, May be like this
initialize a CameraCaptureTask Object
CameraCaptureTask cameracapturetask = new CameraCaptureTask();
cameracapturetask.Completed += new EventHandler<PhotoResult>(cameracapturetask_Completed);
cameracapturetask.Show();
and inside its event
void cameracapturetask_Completed(object sender, PhotoResult e)
{
try
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
img.Source = bmp;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Copy your image in a folder (Images) and set Build Action==Content
// draw an image, set relative source (in project) and add to LayoutRoot.
var i = new Image{
Source = new BitmapImage(
new Uri("/project;component/Images/image.jpg", UriKind.Relative))
};
LayoutRoot.Children.Add(i);
I have a button b3 and an image named pictureBox1 . Im using WPF, however I'm using the winforms openFileDialog instead of the one that comes with WPF :
below is the code that I put inside the click event of my button :
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
}
}
When I click the button and select an icon. The icon doesn't appear in the pictureBox1.
Can someone please explain why the code above doesn't show the icon inside the pictureBox?
You need to assign your image to the pictureBox, else you wont see it on your screen and you only made the image object in memory.
private void b3_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialogIcon = new System.Windows.Forms.OpenFileDialog();
if (openDialogIcon.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(openDialogIcon.FileName, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
pictureBox1.Source = src;
}
}
Try to drag and drop a Image control in your window
...
//imageStretch <- the name of Image control
i.Stretch = Stretch.Uniform;
//int q = src.PixelHeight; // Image loads here
imageStretch.Source = src;
...
Goal:
Save a picture from the harddrive into my WPF application. The picture should be available if copying the WPF application. The address to the picture located in the WPF application should be saved in the database.
Problem:
How should I do it in a course of action?
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "jpg files (*.jpg)|*.jpg|gif files (*.gif)|*.gif|jpeg files (*.jpeg)|*.jpeg";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
txtPicture.Text = filename;
BitmapImage myBitmapImage = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
string sss = myBitmapImage.Format.ToString();
string asd = dlg.SafeFileName.ToString();
}
}
There are several image encoders available. A simple example for PNG files can be found here, or a more complete sample here. The same concept applies for the other supported image file types.