loading pictures from a folder on loading time - wpf

hello i am trying to load images from files in the start of my program and for some unknown reason
whenever i use those lines somehow i am getting thrown out of my loading function
when i press on a button not during the load of the program it does work and i am able to load pictures
this is my loading pictures code :
Image pic = new Image();
string imagePath = String.Format(#"Images\{0}", 1); // this is ofc a file which is inside my debug
pic.Source = new BitmapImage(new Uri(imagePath)); // folder
more info: when i am trying to put this line in my constructor i am getting for some reason an exception:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'yad2.PresentationLayer.MainWindow' that matches the specified binding constraints threw an exception.' Line number '5' and line position '9'.
thanks in advance for your help

"Images\1" is not a valid URI. You can create the Uri by using the FileInfo class:
FileInfo fi = new FileInfo(imagePath);
Uri uri = new Uri(fi.FullName);
pic.Source = new BitmapImage(uri);
Also, a tip to help you debug exceptions in code-behind: Open the Exceptions window (ctrl+alt+e) and check both boxes for Common Language Runtime Exceptions. This will cause execution to break when the error occurs, making it a lot easier to work out what the problem is.

Related

TallComponents.PDF.Document Error: "the document is corrupt, rebuilding failed"

I'm getting exception "the document is corrupt, rebuilding failed",
while instantiating TallComponents.PDF.Document from MemoryStream.
The byteResponse is good. that is, the document is NOT Corrupted.
However, I am running this method in loop to create multiple PDFs.
Am I missing something? Do I need to make any additional checks?
using (Stream stream = new MemoryStream(byteResponse, true))
{
var pdf = new TallComponents.PDF.Document(stream);
Please set stream.Position = 0; before creating the instance of Tall Pdf.
You are doing it but after the instance has been created.

java.lang.NullPointerException: null value in entry: url=null Selenium Webdriver

I am trying the following simple code, which works as per expectation on my localmachine
public class NewTest
{
#Test
public void f() throws IOException
{
Properties obj = new Properties();
FileInputStream fileobj = new FileInputStream("C:\\selenium_Automation\\data_links.properties");
obj.load(fileobj);
System.setProperty("webdriver.chrome.driver", "c:\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(obj.getProperty("crm_url"));
System.out.println("Complete");
}
}
but when i try the same code on a different machine i get the following
FAILED: f
java.lang.NullPointerException: null value in entry: url=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33)
at com.google.common.collect.SingletonImmutableBiMap.<init>(SingletonImmutableBiMap.java:39)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:57)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:80)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:306)
The code works fine if i replace (obj.getProperty("crm_url")) with the actual URL, but i have several different links stored in the properties file and i need them to be read from that place. What i am doing wrong can some please tell me the reason behind the NUll pointer expection for the URL
This is the error you get when you try to add a null object to an immutable map in the google common library. My guess would be that the org.openqa.selenium.remote.RemoteWebDriver.get is attempting to find do that and your file path is null or something similar. I would check the url but that is only a guess.
If I had to guess, I would say that the location of your properties file is different on the other machine.
The issue here I can assume is that the url you are using in the properties file must be wrong or it might have the inverted commas. If it has the inverted commas i.e
url="https://www.google.com" then remove it.
Even I had faced this issue. I deleted the target folder before running the code and my issue was resolved.

WPF MessageBox in App.xaml.cs stops MainWindow initialisation so app never appears but is (apparently) running correctly

There's no error message and no indication why it is not displaying the window. The app initialises App.xaml.cs: App() {} and I can step through the App.xaml file. It gets the startup uri and then... silence. No output in the Output window and no unhandled exception and no window, I can't find where to put a breakpoint to debug as it isn't hitting the start of MainWindow.xaml.cs.
Really confused.
This was working 20m ago.
In that time all I did was add Windows.Office.Interop.Outlook reference. I removed the reference and rebuilt but still the same. Would that cause this problem? Has anyone seen this before? Google isn't helping!
EDIT :
App.xaml.cs:
public App()
{
using (var dbContext = new DBEntities())
{
if (!db.Exists())
{
try
{
db.Database.Create();
MessageBox.Show("Database created"); // this is the problem!!
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
I've added App.xaml.cs, I found that the problem was using a MessageBox to give info (this is still in development!). I'd been meaning to get rid of it and eventually did and my problem went away. This meant I could find relevent Goolge results:
MSDN query and answer for exactly my problem
I will be adding an 'loading window' in between app load and main window load eventually in which I will be able to feedback information using Bindings etc.
Fixed error by removing the MessageBox.Show(..) call. The selected answer from the MSDN URL given in the question states:
"I performed a test based on your description, the applicationi stop at the method : USER32!GetMessageW+0x33, calling USER32!NtUserGetMessage"
I assume this is what was occurring in my case, although I didn't test it.
What happens if you create a new window and set that as the StartupUri?
You also might want to create a new project and make sure that the namespaces referenced in the App.xaml in your existing app haven't somehow been inadvertently edited.

Creating BitmapImage throws missing key exception

I have a view that displays an image with it's title and comment alongside it.
When I load up existing images I utilize this code:
this.ArtifactIdentity = image.ArtifactIdentity;
this.Comment = image.Comment;
this.Name = image.Name;
this.MediaIdentity = image.MediaIdentity;
this.ImageArray = image.Image;
Image = new BitmapImage();
Image.BeginInit();
Image.CacheOption = BitmapCacheOption.None;
Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
Image.StreamSource = new MemoryStream(this.ImageArray);
Image.EndInit();
When I execute the EndInit() it throw the exception missing parameter key. The stack trace shows this:
at System.Collections.Hashtable.ContainsKey(Object key)
at System.Collections.Hashtable.Contains(Object key)
at System.Windows.Media.Imaging.ImagingCache.RemoveFromCache(Uri uri, Hashtable table)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.EndInit()
So can anyone tell me why I am getting this exception when I am using code I've seen many others use with success and yet I get this exception??? I'm at a loss!
This is caused by a bug in WPF, in BitmapImage.FinalizeCreation():
if ((CreateOptions & BitmapCreateOptions.IgnoreImageCache) != 0)
{
ImagingCache.RemoveFromImageCache(uri);
}
If you specify IgnoreImageCache, but don't load from a URI, it breaks.
Just get rid of that flag; it only applies when loading from URLs.
I found an article that was written by Bradley Grainger here and had a list of exceptions caused by loading a BitmapImage. What SLaks stated is correct but in Brad's article he stated that the next exception (No Imaging component suitable to complete...) is frequently showing up on Windows 7 machines. The exception suggests that the metadata is corrupted in some way.
My solution took some testing to confirm but basically if I take the byte stream, save it to a temp file, then use that tempfile to populate the BitmapImage I can successfully load it without exceptions.
It is not the most desired solution but it does the one thing that I needed: it displays the image without exceptions!

First chance FileNotFoundException when calling IsolatedStorageFile.OpenFile

Somewhere in my code, I have this line:
return _store.OpenFile(path, fileMode);
With fileMode being sometimes FileMode.Create and sometimes FileMode.Open.
Everything works well, I always get a valid stream and if necessary, the file is correctly created.
But, I've just discovered in my VS output that I have the following message every time I call the method where the above line is:
A first chance exception of type 'System.IO.FileNotFoundException'
occurred in mscorlib.dll
I get this error when the file is created and I also get this error when the file is overwritten (and obviously exists).
I'm just curious about these errors since everything works perfectly.
Thanks,
EDIT: Same thing with new IsolatedStorageFileStream(...). Everything works fine but I still get the "first chance exception" message.
var isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isfs;
if (!isf.FileExists(_filename))
isfs = new IsolatedStorageFileStream(_filename, System.IO.FileMode.Create, isf);
else
isfs = new IsolatedStorageFileStream(_filename, System.IO.FileMode.Open, isf);
var writer = XmlWriter.Create(isfs);
xml.Save(writer);
writer.Close();
isfs.Close();
isfs.Dispose();
isf.Dispose();
Found the answer.
It's just how VS debugger works: for every exception caught by a {{catch}} block, a "first chance exception" message appears in the VS output.
So here, we can guess that, internally, the {{OpenFile}} method uses a try/catch block to check if the file exists.

Resources