I want to show an image from Disk !
private void Button_Click_3(object sender, RoutedEventArgs e)
{
myImage.Source = new BitmapImage(
new Uri("images\\Countries\\dz.png",UriKind.Relative));
}
I'm sure that the filename is correct but
when I press the button, The image doesn't appear, I also made sure that the image is in the front of all other control and that myImage is its name .
Try this:
private void Button_Click_3(object sender, RoutedEventArgs e)
{
try
{
myImage.Source = new BitmapImage(
new Uri(#"images\Countries\dz.png",UriKind.Relative));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Putting # makes it a literal string (you don't need to provide
escape sequence then)
If the above also doesn't work then it'll show the exception if any
occurs.
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.UriSource = new Uri("images\\Countries\\dz.png", UriKind.Relative);
bitImg.EndInit();
myImage.Source = bitImg;
A few things that may help you:
Instead of:
myImage.Source = new BitmapImage(new Uri("images\\Countries\\dz.png",UriKind.Relative));
Try This:
BitmapImage ImageName = new BitmapImage;
ImageName.BeginInit();
ImageName.UriSource = new Uri(#"images\Countries\dz.png",UriKind.Relative);
ImageName.EndInit();
myImage.Source = ImageName;
See below link (examples section at the bottom of the page) for more info.
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx
If the above doesn't work, we will need you to post more code :). Also, it would help if you specify exactly what you are trying to achieve here.
Try to Debug it.
Put a break point on the line(F9) and hit F5 to start debugging. Check the value of myImage.Source after the line is executed (F10 to step).
You can also use the Immediate Window to test other statements while debugging is paused and see the results.
Also make sure that the images folder is in the same folder as the executable file (in Debug or Release folders, according to your build)
Related
So i have this code which for every string in the array it adds it the file path and uses it as the file path for the image box, this is the code:
private async void Button7_Click(object sender, RoutedEventArgs e)
{
string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png"};
string path = "Assets/Star/";
foreach(string file in images)
{
string thepath = Path.Combine(path,file);
await Task.Delay(46);
BitmapImage Image = new BitmapImage();
Image.UriSource = new Uri(this.BaseUri, thepath);
StarImage.Source = Image;
}
}
Now everytime the new image is loaded in to the StarImage, it flickers, as far as I know their is no way to stop this because it is the effect of loading a new image in the image box, however does anyone know any alternatives to stop this and give the effect of an animation?
I think some sort of buffering technique may reduce or eliminate the flickering problem you have.
I've never done this in c#, but essentially you draw to an image which is not yet visible referred to as the buffer, and then make it visible when the image is completely drawn.
This may help.
try this:
private async void Button7_Click(object sender, RoutedEventArgs e)
{
string[] images = new string[] { "Star_00001.png", "Star_00002.png", "Star_00003.png", "Star_00004.png", "Star_00005.png", "Star_00006.png", "Star_00007.png", "Star_00008.png" };
string path = "Assets/Star/";
foreach (string file in images)
{
string thepath = Path.Combine(path, file);
await Task.Delay(46);
BitmapImage Image = new BitmapImage();
Image.BeginInit();
Image.UriSource = new Uri(this.BaseUri, thepath);
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.EndInit();
StarImage.Source = Image;
}
}
I've been racking my brains for months trying to solve this, and then I found this post. It helped me solve this! I can't understand for the life of me why Microsoft keeps changing the syntax to do things in windows.
How to set Background of a Button without flicker?
I have an issue that I think has not been covered in the multitude of other WPF image loading issues. I am scanning in several images and passing them to a "Preview Page". The preview page takes the image thumbnails and displays what a printout would look like via a generated bitmap.
The weird thing to me is, it will work fine if I run the program the first time. Upon reaching the end of the process and hitting "start over", the preview will return blank. I am creating the BitmapImage in a method that saves the bitmap as a random file name so I do not believe theres a lock on the file the second time around. Also, if I go to look at the temporary file created through explorer, it is drawn correctly so I know the appropriate data is getting to it.
Finally, when I navigate away from this page, I am clearing necessary data. I'm really perplexed and any help would be appreciated.
//Constructor
public Receipt_Form() {
InitializeComponent();
printData = new List<Object>();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e) {
// populates global variable fileName
var task = System.Threading.Tasks.Task.Factory.StartNew(() => outputToBitmap()); task.ContinueWith(t => setImage(fileName),
System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
// I started the image creation in a separate thread because I
// thought it may be blocking the UI thread, but it didn't matter
}
private void setImage(string imageURI) {
BitmapImage image;
using (FileStream stream = File.OpenRead(imageURI)) {
image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
}
receiptPreview.Source = image;
//this works the first iteration but not the second, though the temp file is created successfully
}
Found the issue - the Modern UI container was getting cleared when transitioning off the page.
I am working on a C# project, and I am having a strange problem. I have an Image control and I am trying to display an image within it. There is no error or exception thrown, but the image doesn't display.
Below is the code I am using:
BitmapImage image = new BitmapImage();
image.UriSource = new Uri("images\\low_battery.png", UriKind.Relative);
imageIcon.Source = image;
imageIcon.Visibility = System.Windows.Visibility.Visible;
I have the directory image and the image file low_battery.png running in the same directory as to where the executable is running from.
Thanks for any help you can provide.
A few suggestions:
1) You can get the exception from WPF if you subscribe to the ImageFailed even on imageIcon and the and DownloadFailed event on BitmapImage. Then put breakpoints in those methods and see what it says.
2) Before setting UriSource, call image.BeginInit(). After setting UriSource, call image.EndInit(). To help debug the problem, you can subscribe to the ImageFailed event on imageIcon and DownloadFailed on BitmapImage. So the final code looks like this:
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri("/images/low_battery.png", UriKind.Relative);
image.DownloadFailed += image_DownloadFailed;
image.EndInit();
imageIcon.ImageFailed += imageIcon_ImageFailed;
imageIcon.Source = image;
imageIcon.Visibility = System.Windows.Visibility.Visible;
and further down:
void imageIcon_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// Breakpoint here
}
void image_DownloadFailed(object sender, ExceptionEventArgs e)
{
// Breakpoint here
}
3) Another way to do this is to embed images directly into your project. Do that by adding an images folder into your project, then adding the image, and setting the type to "Resource"
Either call image.BeginInit() before setting image.UriSource and image.EndInit() afterwards, or simply use the BitmapImage constructor that takes an Uri as argument:
var uri = new Uri("images\\low_battery.png", UriKind.Relative);
imageIcon.Source = new BitmapImage(uri);
Okay, I do not know what is causing the crash of my app and I simply do not understand what is happening. I will explain shortly what my app CAN do and what is my problem.
Furthermore I read barely any topic about that on here, and was clicking my way through different google sites.
I am not finding a solution, so I have to ask!
I have an image set as Background -> Works fine.
I have a TextBlock which displays different text every 15 seconds, controlled by a timer, each text is saved in a list! -> Works fine.
I have a fade in/fade out of that text -> Works fine.
I have an application bar at the bottom -> Works fine.
Chaing one explicit picture with that peace of code -> Works fine.
private void Appearance_Click(object sender, EventArgs e)
{
Hintergrund.Source = new BitmapImage(new Uri("/Pictures/StarsNight19.jpg", UriKind.Relative));
}
Well I have around 20 different Images, all have pretty the same names, saved in a folder in my project. The path is as shown in the code fragment: /Pictures/StarsNightXX.jpg
Build Action is set to: CONTENT (well tried everything basically..)
Copy To Output Directory is set to: Copy Always.
Now here is my problem.
I saved the names of my images in a List.
.....
pictures.Add("StarsNight4.jpg");
pictures.Add("StarsNight5.jpg");
pictures.Add("StarsNight6.jpg");
....
I use the same operation as before, wanting it to change the image when clicked on the nice little button in my application bar:
private void Appearance_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int next = rnd.Next(0, pictures.Count - 1);
background.Source = new BitmapImage(new Uri("/Pictures/"+pictures.ElementAt(next), UriKind.RelativeOrAbsolute));
}
BOOM APP CRASHES
I just can not figure out where the problem is.
Changing it by writing an explicit name as shown at the beginning is working out fine...
Maybe someone can tell me if the list is causing a problem?
I just can not figure it out.
"looping it" does not work out either:
int i = 0;
private void Appearance_Click(object sender, EventArgs e)
{
if (i >= pictures.Count) i = 0;
background.Source = new BitmapImage(new Uri("/Pictures/" + pictures.ElementAt(i), UriKind.RelativeOrAbsolute));
i++;
}
Because I am testing my App directly on my WP I do not know what kind of exception I get.
No way compiling and testing it on my computer just to let you know.
...
Losing my mind right here.
Kindly try this source code, I've tried to dispose of the object which are created in the source below, use your list and other code such as loop as it is.
private static BitmapImage bi = null;//this line at the top, not in function
private static Image si = null;//this line at the top, not in function
if bi!=null)
{
bi.Dispose();
bi = null;
}
if si!=null)
{
si.Dispose();
si = null;
}
BitmapImage bi = new BitmapImage();
Image si = new Image();
bi.BeginInit();
bi.UriSource = new Uri(#"/img/img1.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
si.Source = bi;
This seems like a serious bug :
private void LayoutRoot_Drop(object sender, DragEventArgs e)
{
if ((e.Data != null) && (e.Data.GetDataPresent(DataFormats.FileDrop)))
{
FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);
using (FileStream fileStream = files[0].OpenRead())
{
//Code reaching this point.
BitmapImage bmpImg = new BitmapImage();
bmpImg.ImageOpened += new EventHandler<RoutedEventArgs>(bmpImg_ImageOpened);
bmpImg.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bmpImg_ImageFailed);
try
{
bmpImg.SetSource(fileStream);
}
catch
{
//Code dosen't reach here.
}
}
}
}
void bmpImg_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
//Code dosen't reach here.
}
void bmpImg_ImageOpened(object sender, RoutedEventArgs e)
{
//Code dosen't reach here.
}
I am experiencing a very strange behivour. Running this code on my computer, it works - when you drag a JPG on the LayoutRoot I can break inside bmpImg_ImageOpened().
But on a different machine it won't work - when dragging a JPG, I can break in the drop event but after SetSource() nothing happens : no exceptions are thrown, and non of the callbacks are invoked.
I tried it on another machine and it also didn't work.
edit:
On all of the machines, when adding an Image class and setting it's Source property to the bitmapImage, the image is shown fine. so I guess it's an issue with the callbacks. This is not enough because I still need those events.
I am banging my head here, what could it be ?
This is simply how Silverlight has always behaved. ImageOpened only fires if the image is downloaded and decoded (i.e. using Source). It does not fire when using SetSource. If you need access to the dimensions after loading your image either use WriteableBitmap for the PixelWidth and PixelHeight properties (instead of BitmapImage) or do something like:
img.Source = bmpImg;
Dispatcher.BeginInvoke(() =>
{
FakeImageOpened(); // Do logic in here
});
You have to set
bitmapImage.CreateOptions = BitmapCreateOptions.None;
Then the ImageOpened event is fired. This is because the default Options are CreateDelayed
Greetings
Christian
http://www.wpftutorial.net