I have images that is not located in resources but on the disk. The folder is relative to application. I used:
Overview_Picture.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../MyImages /myim.jpg", Directory.GetCurrentDirectory())));
Overview_Picture.Source = new BitmapImage(uriSource);
But those type of code created many problems and messed up GetCurrentDirectory returns that sometime ok and some times not.
So, MyImages folder is located next to Debug folder, how can I use them images there and not as I done, In some other more right way?
As mentioned oftentimes here on SO, the GetCurrentDirectory method does by definition not always return the directory your assembly resides in, but the current working directory. There is a big difference between the two.
What you need is the current assembly folder (and the parent of that). Also, I'm not sure whether it is wanted that the pictures are one folder above the installation folder (which is basically what you're saying when you say they are one level above the Debug folder - in real life that would be one folder above the folder the application is installed to).
Use the following:
string currentAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string currentAssemblyParentPath = Path.GetDirectoryName(currentAssemblyPath);
Overview_Picture.Source = new BitmapImage(new Uri(String.Format("file:///{0}/MyImages/myim.jpg", currentAssemblyParentPath)));
Also, there's a stray space after MyImages, which I removed.
An alternative to constructing an absolute Uri from a relative file path would be to just open a FileStream from the relative path, and assign that to the BitmapImage's StreamSource property. Note however that you also have to set BitmapCacheOption.OnLoad when you want to close the stream right after initializing the BitmapImage.
var bitmap = new BitmapImage();
using (var stream = new FileStream("../MyImages/myim.jpg", FileMode.Open))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
bitmap.Freeze(); // optional
}
Overview_Picture.Source = bitmap;
Related
I'm trying to programmically delete a file, but the file is apparently being used by another process (which happens to be my program). Basically, the program loads images from a folder by using FromUri to create a Bitmap, which is then loaded into an Image array, which in turn becomes the child of a stackpanel. Not very efficient, but it works.
I've tried clearing the stackpanel's children, and making the images in the array null, but I'm still getting the IOException telling me that the file is being used by another process.
Is there some other way to remove the file from my application's processes?
it may be Garbage Collection issue.
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(picturePath);
In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:
string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();
Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:
string filename = ...
BitmapImage image = new BitmapImage();
using (var stream = File.OpenRead(filename))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
Another way is to delete file. Load your file using FileStream class and release an file
through stream.Dispose();
it will never give you the Exception "The process cannot access the file '' because it is being used by another process."
using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
pictureBox1.Image = Image.FromStream(stream);
stream.Dispose();
}
// delete your file.
File.Delete(delpath);
var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName); //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this,
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!
I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.
I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:
MyDialog.AutoUpgradeEnabled = false;
I commented out that line and it was resolved.
In my case, I started a new process of devenv.exe opening a temporary solution file. After the process was ended, I found I could not delete the directory for few minutes. Checking with "resmon", resource monitor, I found it was a executable called PerfWatson2.exe that was using the temp file. Looking at the site, PerfWatson is actually a Visual Studio Customer Experience Improvement Program from MicroSoft. It will lock the file or directory you temporarily used even after you have ended the VS IDE.
The solution is to disable the Visual Studio Customer Experience Improvement Program, see this. This shoudln't be an issue after your app is publihsed. But it is quite annoying during debuging.
I'm trying to programmically delete a file, but the file is apparently being used by another process (which happens to be my program). Basically, the program loads images from a folder by using FromUri to create a Bitmap, which is then loaded into an Image array, which in turn becomes the child of a stackpanel. Not very efficient, but it works.
I've tried clearing the stackpanel's children, and making the images in the array null, but I'm still getting the IOException telling me that the file is being used by another process.
Is there some other way to remove the file from my application's processes?
it may be Garbage Collection issue.
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(picturePath);
In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:
string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();
Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:
string filename = ...
BitmapImage image = new BitmapImage();
using (var stream = File.OpenRead(filename))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
Another way is to delete file. Load your file using FileStream class and release an file
through stream.Dispose();
it will never give you the Exception "The process cannot access the file '' because it is being used by another process."
using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
pictureBox1.Image = Image.FromStream(stream);
stream.Dispose();
}
// delete your file.
File.Delete(delpath);
var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName); //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this,
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!
I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.
I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:
MyDialog.AutoUpgradeEnabled = false;
I commented out that line and it was resolved.
In my case, I started a new process of devenv.exe opening a temporary solution file. After the process was ended, I found I could not delete the directory for few minutes. Checking with "resmon", resource monitor, I found it was a executable called PerfWatson2.exe that was using the temp file. Looking at the site, PerfWatson is actually a Visual Studio Customer Experience Improvement Program from MicroSoft. It will lock the file or directory you temporarily used even after you have ended the VS IDE.
The solution is to disable the Visual Studio Customer Experience Improvement Program, see this. This shoudln't be an issue after your app is publihsed. But it is quite annoying during debuging.
I'm trying to programmically delete a file, but the file is apparently being used by another process (which happens to be my program). Basically, the program loads images from a folder by using FromUri to create a Bitmap, which is then loaded into an Image array, which in turn becomes the child of a stackpanel. Not very efficient, but it works.
I've tried clearing the stackpanel's children, and making the images in the array null, but I'm still getting the IOException telling me that the file is being used by another process.
Is there some other way to remove the file from my application's processes?
it may be Garbage Collection issue.
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(picturePath);
In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:
string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();
Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:
string filename = ...
BitmapImage image = new BitmapImage();
using (var stream = File.OpenRead(filename))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
Another way is to delete file. Load your file using FileStream class and release an file
through stream.Dispose();
it will never give you the Exception "The process cannot access the file '' because it is being used by another process."
using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
pictureBox1.Image = Image.FromStream(stream);
stream.Dispose();
}
// delete your file.
File.Delete(delpath);
var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName); //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this,
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!
I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.
I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:
MyDialog.AutoUpgradeEnabled = false;
I commented out that line and it was resolved.
In my case, I started a new process of devenv.exe opening a temporary solution file. After the process was ended, I found I could not delete the directory for few minutes. Checking with "resmon", resource monitor, I found it was a executable called PerfWatson2.exe that was using the temp file. Looking at the site, PerfWatson is actually a Visual Studio Customer Experience Improvement Program from MicroSoft. It will lock the file or directory you temporarily used even after you have ended the VS IDE.
The solution is to disable the Visual Studio Customer Experience Improvement Program, see this. This shoudln't be an issue after your app is publihsed. But it is quite annoying during debuging.
I am trying to capture the screen and save that image into a folder, everything works fine in my local machine. I've created a setup and installed it on another machine and when i run my application it says that path is incorrect. How can i solve this code below shows how i save image and retrieve it
public void capture_screen()
{
Size s = Screen.PrimaryScreen.Bounds.Size;
Bitmap bmp = new Bitmap(s.Width, s.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 90, s);
System.IO.Stream stream = new System.IO.MemoryStream();
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Save(#"C:\Users\XXXX\Documents\TESTs\MyImage.jpg");
stream.Position = 0;
pbScreenShots.SizeMode = PictureBoxSizeMode.StretchImage;
img =bmp;
pbScreenShots.Image = bmp;
}
How to change this path to make my setup work in any machine?
Why not just write it to the temp folder:
bmp.Save(Path.GetTempPath());
You can try Special Folders but you might need to ensure you have access to it
myDocsFilePath = Environment.SpecialFolder.MyDocuments
Other than this, you must ensure the computer has a "TESTs" folder in my documents, and that "MyImage" doesn't already exist as you may overwrite a valued image. In short, you are probably better taking al-Khwārizmī's answer (+1!), then prompting the user to choose their own filename and destination, unless you require the picture elsewhere.
Use the Environement.UserName property:
bmp.Save(string.Format(#"C:\Users\{0}\Documents\TESTs\MyImage.jpg", Environment.UserName));
Use Envrinment.GetFolderPath(Environment.SpecialFolder.MyDocuments) to get the user's "my document" folder, use Path.Combine to add a sub folder (you must make sure that folder exists or create it, obviously) or file name.
I have a problem with displaying certain images in my application using C#. I am using the Image class to specify the location and the BitmapImage to specify the source. The UriSource is relative and I just specify the name. It worked for some images, but for others, the image simply does not appear. My image instance is 35x35 big and another is 100x100 big (pixels).
Anyone knows why this might be occurring and how to fix it?
Thanks.
Here's the code I used:
Image removeImage = new Image();
removeImage.HorizontalAlignment = HorizontalAlignment.Left;
removeImage.VerticalAlignment = VerticalAlignment.Top;
removeImage.Margin = new Thickness(490, 10, 0, 0);
removeImage.Width = 35;
removeImage.Height = 35;
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("delete.png", UriKind.RelativeOrAbsolute);
source.EndInit();
removeImage.Source = source;
removeImage.Stretch = Stretch.None;
removeImage.Visibility = Visibility.Visible;
removeImage.MouseDown += new MouseButtonEventHandler(removeImage_MouseDown);
Not sure about the location of image files. If images are in your current project folder then you have to set Copy To Output Directory=Copy Always property of image file from Properties Windows.
The best way that I know of to diagnose a problem like that (assuming a quick peer review of the code gets you nowhere), is to use ProcessMonitor: http://technet.microsoft.com/en-us/sysinternals/bb896645
You can use this tool to monitor all of the file activity on your machine (make sure to use the include/exclude filters to limit the noise).
It's very likely that the reason that the images are not showing up is because your application is looking for them in the wrong place (either they didn't get copied, or the relative path is off).
ProcessMonitor will log every attempt that Windows makes to access your .jpg (whether it fails or succeeds). If you search for your file name in the log, you should find it, probably along with an error message, and the full path that Windows was using to open the file.
The most common results I see are
Path that was actually being used was different from the path you needed.
The path was correct, but your files weren't there (build/copy/install problem)
The path was correct, but your web app did not have permissions to read the file.
In all those cases, ProcessMonitor will show you what happened.