I am looking for a code snippet which opens a image, creates a clone of this bitmap in memory disposes this opened image so that all handles are closed.

Usually we can open a bitmap using this

Dim MyBit as new bitmap("C:\Image file.bmp")
dim ClonedBit as bitmap = MyBit.clone
MyBit.dispose

the following code however keeps the Image File locked until the application is running, any suggestions how to dispose it properly. (So that no handle is left open )

Things i already tried:
use dispose method of bitmap <= doesnt work (need to write implementation)
Using Block also doesnt work.

Does this proof of concept fit your needs?

static void Main(string[] args)
        {
            string fileName = @"C:\Work\test.bmp";
            string fileNameCopy = @"C:\Work\test2.bmp";

            Byte[] buffer = File.ReadAllBytes(fileName);
            MemoryStream stream = new MemoryStream(buffer);

            try
            {
                File.Delete(fileName);
                Console.WriteLine("Original file deleted.");
            }
            catch
            {
                throw ;
            }

            Bitmap bitmap = (Bitmap)Bitmap.FromStream(stream);
            bitmap.Save(fileNameCopy);
            bitmap.Dispose();
            stream.Dispose();
            Console.Write("New file saved.");
            Console.Read();
        }

Oops, forgot what board I was on... here's a VB version

Sub Main()

        Dim fileName As String = "C:\Work\test.bmp"
        Dim fileNameCopy As String = "C:\Work\test2.bmp"

        Dim buffer As Byte() = File.ReadAllBytes(fileName)
        Dim stream As MemoryStream = New MemoryStream(buffer)

        Try
            File.Delete(fileName)
            Console.WriteLine("Original file deleted.")
        Catch ex As Exception
            Throw ex
        End Try

        Dim myBitmap As Bitmap = CType(Bitmap.FromStream(stream), Bitmap)
        myBitmap.Save(fileNameCopy)
        myBitmap.Dispose()
        stream.Dispose()
        Console.WriteLine("New file saved.")
        Console.Read()

    End Sub

Does this proof of concept fit your needs?

static void Main(string[] args)
        {
            string fileName = @"C:\Work\test.bmp";
            string fileNameCopy = @"C:\Work\test2.bmp";

            Byte[] buffer = File.ReadAllBytes(fileName);
            MemoryStream stream = new MemoryStream(buffer);

            try
            {
                File.Delete(fileName);
                Console.WriteLine("Original file deleted.");
            }
            catch
            {
                throw ;
            }

            Bitmap bitmap = (Bitmap)Bitmap.FromStream(stream);
            bitmap.Save(fileNameCopy);
            bitmap.Dispose();
            stream.Dispose();
            Console.Write("New file saved.");
            Console.Read();
        }

Oops, forgot what board I was on... here's a VB version

Sub Main()

        Dim fileName As String = "C:\Work\test.bmp"
        Dim fileNameCopy As String = "C:\Work\test2.bmp"

        Dim buffer As Byte() = File.ReadAllBytes(fileName)
        Dim stream As MemoryStream = New MemoryStream(buffer)

        Try
            File.Delete(fileName)
            Console.WriteLine("Original file deleted.")
        Catch ex As Exception
            Throw ex
        End Try

        Dim myBitmap As Bitmap = CType(Bitmap.FromStream(stream), Bitmap)
        myBitmap.Save(fileNameCopy)
        myBitmap.Dispose()
        stream.Dispose()
        Console.WriteLine("New file saved.")
        Console.Read()

    End Sub

Yes that does it , infact after i posted this question here on daniweb, i found the solution and i modded it to make it fit into 2 lines here :

Dim ThisMemoryStream As New MemoryStream(My.Computer.FileSystem.ReadAllBytes("File Path"))
               
 Dim ThisBitmap As Bitmap = New Bitmap(ThisMemoryStream)

So now you get a bitmap which isnt at all connected to a File on hard drive rather a Stream in memory.

apegram Thanks for posting well described code, i appreciate it.

i have tried the above bitmap class, now i can open an image before the programme start. great work. thank you for posting the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.