File.Delete failing when Image.FromFile was called prior it, despite making copy of loaded image and destroying original one

前端 未结 5 1033
忘了有多久
忘了有多久 2020-11-30 10:56

UPDATED

I used below solutions (loading Image from stream), but get new problem. img object is absolutely correct Image class instance with all fiel

5条回答
  •  旧时难觅i
    2020-11-30 11:04

    This should do the trick:

      Image img = null;
      using (var stream = File.OpenRead(path)) {
        img = Image.FromStream(stream);
      }
      File.Delete(path);
    

    UPDATE: Don't use the code above!

    I've found the related knowledge base article: http://support.microsoft.com/?id=814675

    The solution is to really copy the bitmap as outlined in the article. I've coded the two ways that the article mentions (the first one was the one you were doing, the second one is the one in your answer, but without using unsafe):

    public static Image CreateNonIndexedImage(string path) { 
      using (var sourceImage = Image.FromFile(path)) { 
        var targetImage = new Bitmap(sourceImage.Width, sourceImage.Height, 
          PixelFormat.Format32bppArgb); 
        using (var canvas = Graphics.FromImage(targetImage)) { 
          canvas.DrawImageUnscaled(sourceImage, 0, 0); 
        } 
        return targetImage; 
      } 
    } 
    
    [DllImport("Kernel32.dll", EntryPoint = "CopyMemory")] 
    private extern static void CopyMemory(IntPtr dest, IntPtr src, uint length); 
    
    public static Image CreateIndexedImage(string path) { 
      using (var sourceImage = (Bitmap)Image.FromFile(path)) { 
        var targetImage = new Bitmap(sourceImage.Width, sourceImage.Height, 
          sourceImage.PixelFormat); 
        var sourceData = sourceImage.LockBits(
          new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 
          ImageLockMode.ReadOnly, sourceImage.PixelFormat); 
        var targetData = targetImage.LockBits(
          new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 
          ImageLockMode.WriteOnly, targetImage.PixelFormat); 
        CopyMemory(targetData.Scan0, sourceData.Scan0, 
          (uint)sourceData.Stride * (uint)sourceData.Height); 
        sourceImage.UnlockBits(sourceData); 
        targetImage.UnlockBits(targetData); 
        targetImage.Palette = sourceImage.Palette;
        return targetImage; 
      } 
    } 
    

提交回复
热议问题