1
 public static void CopyImage(Image picToSave, string name)
    {
        if (picToSave.Source != null)
        {
            BitmapImage src = (BitmapImage)picToSave.Source;
            if (!Directory.Exists("Images"))
            {
                Directory.CreateDirectory("Images");
            }

            FileStream stream = new FileStream("Images/" + name + ".jpg", FileMode.Create);
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(src));
            encoder.Save(stream);
            stream.Close();
        }



    }

The problem occurs when i choose a file which already exists in the /Images directory, i guess it just cant overwrite, The exception is thrown at the "FileStream"" line (FileMode.Create I guess).

If i Choose a file which isn't in the /Images directory it works fine and copies the file to the Images Directory like it should..

Thank you :)

Nadav
  • 555
  • 3
  • 10
  • 19

4 Answers4

7

How did you load the image in the first place? If you didn't change the default value for CacheOption, the file is locked by the BitmapImage object. You need to specify BitmapCacheOption.OnLoad:

BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = imageUri;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • My image is "Image" Type and not BitmapImage so I can't use all this, is there a way for "Image" type? – Nadav Dec 24 '10 at 17:10
  • Thanks Thomas :) I had a similar issue and you were the only one pointing to the real issue. I wanted to delete an image displayed in an image control. Till I saw your post I was trying to make the image source as null and then do System.GC.Collect() etc, but after I saw your post I realized that the problem was with the way I loaded the image initially! – Blesson Jose Jul 27 '15 at 15:52
3

Assuming you created Bitmaps from all the images in the image folder this sounds like a known problem with the Bitmap class - it keeps a file lock on the file you created it from until you call dispose. Also see this thread: .NET app locks file.

Hans Passant offers the following workaround in this thread: Loading a file to a Bitmap but leaving the original file intact

public static Image LoadImageNoLock(string path) {
        using (var ms = new MemoryStream(File.ReadAllBytes(path))) {
            return Image.FromStream(ms);
        }
    }
Community
  • 1
  • 1
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • The compiler doesn't recognize "FromStream" – Nadav Dec 24 '10 at 17:09
  • you need a reference to System.Drawing to use the Image class as in your sample - http://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream.aspx – BrokenGlass Dec 24 '10 at 17:25
  • 2
    The OP is using WPF, which doesn't use System.Drawing (the Image class in the code is System.Windows.Controls.Image). Anyway, you don't need to load the image into a MemoryStream. You can just open a FileStream, load the image from it, and close the stream – Thomas Levesque Dec 25 '10 at 19:09
2

Just thought I would post something that worked for me from @Thomas Levesque's comment.

FileStream stream = new FileStream(imageLocation, FileMode.Open);
Image image = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.EndInit();
image.Source = bi;
image.Height = 15;
btn.Content = image;
stream.Close();
stream.Dispose();
SpoiledTechie.com
  • 10,515
  • 23
  • 77
  • 100
0

try with some modifications in your code

set the write permission to your opened file

FileStream stream = new FileStream("Images/" + name + ".jpg", FileMode.Create, FileAccess.Write);
Binil
  • 6,445
  • 3
  • 30
  • 40