Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1059339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:09:22+00:00 2026-05-16T18:09:22+00:00

UPDATED I used below solutions (loading Image from stream), but get new problem. img

  • 0

UPDATED

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

img.Save("path/to/new/image.bmp");

on it results in new exception for GDI+ (System.Runtime.InteropServices.ExternalException, in GDI+ interface) – I get error message but is in polish, am not sure how to translate it.

Original question

I have problem with C# .NET Framework 2.0

Basically I’am trying to achieve:

Image img = Image.FromFile("Path/To/Image.bmp");
File.Delete("Path/To/Image.bmp"); // Exception, the file is in use!

It is important for me to keep copy of image in memory when original file was deleted. I though it is somehow odd that .NET still lock file on hard disc despite it is no longer required for any operation (entire image is now in memory, isn’t it?)

So I tried this solution:

Image img = new Image(Image.FromFile("Path/To/Image.bmp")); // Make a copy
                    // this should immiedietaly destroy original loaded image
File.Delete("Path/To/Image.bmp"); // Still exception: the file is in use!

I can do:

Image img = null;
using(Image imgTmp = Image.FromFile("Path/To/Image.bmp"))
{
    img = new Bitmap(imgTmp.Width, imgTmp.Height, imgTmp.PixelFormat);
    Graphics gdi = Graphics.FromIage(img);
    gdi.DrawImageUnscaled(imgTmp, 0, 0);
    gdi.Dispose();
    imgTmp.Dispose(); // just to make sure
}
File.Delete("Path/To/Image.bmp"); // Works fine
// So I have img!

But this seems to me almost like using nuke to kill bug… and raises another problem: GDI poorly support Drawing palette-based images onto each other (and palette ones are majority in my collection).

Am I doing something wrong? Is any better way to have Image in memory and original file deleted from hard disk?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:09:23+00:00Added an answer on May 16, 2026 at 6:09 pm

    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; 
      } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Update: I've found the API reference. The method used is below] <?php wp_delete_post( $postid,
Why isn't 'ALT' (variable used to determine row colour) being updated (see pic) Here
Update 2 : So I never have used the debug function in firebug, but
I'm new to jQuery and JavaScript I have managed to get a small snippet
UPDATE: I have realized the problem below is not possible to answer in its
UPDATED - please read further details below original question I have a select form
I've set a variable so it can be used from the controller to pass
The code below models a simple SIR model (used in disease control) in Mathematica.
UPDATED: I used daemon_generator in a Rails 2.3 app to create a daemon. Per
I already tried references from similar question on SO, but hasn't got the appropriate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.