Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Picture and stream

Stephand DavidStephand David Posts: 1Member
In my application, I've created a zip where I store several pictures (256 colors gif)
When my application is loaded, I want to open the zip and load the gif in memory, but it doesn't work as I want, and I'd like some help.

Here is what I do( it's very simplified)

Method 1:
FileStream stream
// Read the contains of the zip with this stream,
// it create Foo.gif (this part works)
stream.Close();
Bitmap Picture = new Bitmap ("Foo.gif");
File.Delete("Foo.gif");

I don't want to keep the file on the hard drive, so I delete it once it is loaded. Pb : I got an error message at runtime "Foo.gif used by another process", and I cannot delete it. But the Stream is supposed to be closed. How can I delete this file?

Method 2:
Instead of using a FileStream to read the zip and save it, I use a MemoryStream, and I create the picture from this stream. It works. Almost. Instead of a 256 colors picture, It create a 32bppArgb picture, so it takes a lot of memory.

Any idea of how I can access the picture in my zip, use them as 256 colors gif, and still be able to delete the picture once I'm done loading it?


Comments

  • pingpongpingpong Posts: 937Member
    : Method 1:
    : FileStream stream
    : // Read the contains of the zip with this stream,
    : // it create Foo.gif (this part works)
    : stream.Close();
    : Bitmap Picture = new Bitmap ("Foo.gif");
    : File.Delete("Foo.gif");
    :
    : I don't want to keep the file on the hard drive, so I delete it once it is loaded. Pb : I got an error message at runtime "Foo.gif used by another process", and I cannot delete it. But the Stream is supposed to be closed. How can I delete this file?

    Afraid there's nothing you can do here. This is the way GDI+ work (and therefore .NET). When you load from a "stream" be it a file or a memory stream, the stream must stay alive as long as the image is alive.

    :
    : Method 2:
    : Instead of using a FileStream to read the zip and save it, I use a MemoryStream, and I create the picture from this stream. It works. Almost. Instead of a 256 colors picture, It create a 32bppArgb picture, so it takes a lot of memory.
    :

    Never heard of this before. As a small test, run this code, should report 8 bpp indexed, if not, you might have a problem somewhere else in the code. Care to post so we can take a look at it?
    [code]
    using System;
    using System.IO;
    using System.Drawing;

    // change to a gif file on your system
    using(FileStream fs = File.OpenRead(@"x:lalala.gif"))
    {
    using(MemoryStream ms = new MemoryStream())
    {
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int count;

    do
    {
    count = fs.Read(buffer, 0, bufferSize);
    if(count > 0)
    ms.Write(buffer, 0, count);
    }
    while(count > 0);

    fs.Close();

    using(Bitmap bitmap = Bitmap.FromStream(ms) as Bitmap)
    Console.WriteLine("PixelFormat is {0}", bitmap.PixelFormat);
    }
    }
    [/code]

Sign In or Register to comment.