Im having a sort of an editor, that has some strings and a picture displayed in the ui and iam saving the data in a textfile and a detached .jpg file with the same name.
Now when iam trying to override the .jpg file by File.Replace
, it says that the picture is blocked by another process which is obviously the process of my own application.
I hope that the memorystream can fix this because it is handled in the RAM. So i would like to load all the jpg files by a memorystream but i dont understand how to load anything with the memorystream, if that is a possible solution to my problem at all..
Edit: Basicly the code snippit this is about is this one :
private void CopyPicture(bool PictureHasChanged)
{
try
{ //kopiere die datei nur, wenn sie nicht bereits vorhanden ist.
if (File.Exists(TargetFolder + Exercise.Name + ".jpg") == false)
{//kopiert das neue bild in das zielverzeichnis
File.Copy(Exercise.Bild.UriSource.LocalPath, TargetFolder + Exercise.Name + ".jpg");
}
else
{
//wenn das Bild einer bestehenden übung geändert wurde
if (PictureHasChanged)
{
//überprüft ob eine datei mit dem namen existiert
if (File.Exists(TargetFolder + Exercise.Name + ".jpg") == true)
{//löscht die existente datei
File.Replace(Exercise.Bild.UriSource.LocalPath, TargetFolder + Exercise.Name + ".jpg", TargetFolder + Exercise.Name + ".jpg");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.Source);
return;
}
}
and i have the picture stored internal in a simple list as a bitmapimage by : new BitmapImage(new Uri(f.FullName.ToString().Remove(f.FullName.Length - 4, 4) + ".jpg",UriKind.RelativeOrAbsolute))
i hope that this helps to understand the problem better
EDIT 2: now iam doing the following in order to load the Picture:
FileStream fsSource = new FileStream(JpgTarget, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fsSource.Length];
using (fsSource)
{
// Read the source file into a byte array.
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
//numBytesToRead = bytes.Length;
}
BitmapImage Image = new BitmapImage();
//erstellt das bitmap für die liste
using (MemoryStream Memstream = new MemoryStream(bytes))
{
Image.BeginInit();
Image.StreamSource = Memstream;
Image.CacheOption = BitmapCacheOption.OnLoad;
Image.EndInit();
Image.Freeze();
}
fsSource.Close();
But it Keeps telling me that The Pictrue, iam trying to overwrite is already in use by another process.
Edit 3: I tried to use pennie Pet's solution and ended up with this, having the same problem with the locked file:
Bitmap newBitmap = GetImageFromByteArray(File.ReadAllBytes(JpgTarget));
using (MemoryStream memory = new MemoryStream())
{
newBitmap.Save(memory, newBitmap.RawFormat);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
//fügt der liste die aus der textdatei gelesene übung hinzu
List.Add(new Uebung(text, Sitting, wdh, bitmapImage, f.Name.Substring(0, f.Name.Length - 4)));
}
See the GetImageFromByteArray methode in PenniePet's post.
Last Edit:
It was totally my fault and it really embarrass me, i forgot that i was Loading the Image at another point. Since i fixed it, the locked-problem does not exist anymore.
I picked PenniePete's Awnser as correct, because iam currently using it, as it was the last one i tried, and he also opened my eyes to my fail.
I hope you other guys wont be mad. Thanks for all of your help!
If I understand right, you want to read the .jpg file, and convert it into a .Net Bitmap object in a way that guarantees that the .jpg file does not end up being locked. If so, here are some code snippets from a previous answer of mine: https://stackoverflow.com/a/16576471/253938
and