Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Get bitmap file name

I use this line below for create my Bitmap:

Bitmap b = new Bitmap(@"C:\<file name>");

After I modify this bitmap and I want to save it as in line below:

b.Save(@"C:\\<other file name>")

My question is - how to get the file name bitmap from property.
Simply put, I need to save the bitmap with the same name with whom I had initiated it.

Thanks

Upvotes: 8

Views: 22304

Answers (6)

Irvingz
Irvingz

Reputation: 119

A possible work around is to use the Bitmap's Tag property to store the path on creation..

string pathToBitmap = @"C:\myImage.bmp";
Bitmap myBitmap = new Bitmap(pathToBitmap) { Tag = pathToBitmap };

..and then retrieve the path later..

string pathFromBitmap = (string)myBitmap.Tag;

..just make sure to cast the Tag as string, and also to copy the Tag to any instances made from the source bitmap.

Upvotes: 2

JumpingJezza
JumpingJezza

Reputation: 5665

Another way to get around the problem of locking the file that Hans describes is to freeze the image.

public PhotoImageSource GetImage()
{
    string filename = "c:\Images\myimage.png";
    var image = new BitmapImage();
    using (var stream = new FileStream(fileName, FileMode.Open))
    {
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.StreamSource = stream;
        image.EndInit();
    }
    image.Freeze(); //prevent error "Must create DependencySource on same Thread as the DependencyObject"
    return new PhotoImageSource(image, filename);
}

public class PhotoImageSource
{
    public PhotoImageSource(ImageSource image, string filename)
    {
        Image = image;
        Filename = filename;
    }
    public ImageSource Image { get; set; }
    public string Filename { get; set; }
}

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941417

The upvoted answer is going to get you into trouble. Loading a bitmap from a file puts a lock on the file. You cannot save it back without disposing the bitmap first. Which is a chicken and egg problem which you can't resolve without cloning the bitmap. Which is a bit tricky in itself, the Bitmap.Clone() method is an optimized version that uses the same memory section that put the lock on the file in the first place. So doesn't actually releases the lock.

Here's a little class that takes care of creating a deep clone and memorizes the path and format of the original bitmap:

    class EditableBitmap : IDisposable {
        public EditableBitmap(string filepath) {
            using (var bmp = new Bitmap(filepath)) {
                this.bitmap = new Bitmap(bmp);
                this.bitmap.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
            }
            this.path = System.IO.Path.GetFullPath(filepath);
            this.format = bitmap.RawFormat;
        }

        public Bitmap Bitmap { get { return bitmap; } }

        public void Save() {
            bitmap.Save(path, format);
            this.Dispose();
        }

        public void Dispose() {
            if (bitmap != null) {
                bitmap.Dispose();
                bitmap = null;
            }
        }
        private Bitmap bitmap;
        private System.Drawing.Imaging.ImageFormat format;
        private string path;
    }

And use it like this:

        using (var bmp = new EditableBitmap(@"c:\temp\test.png")) {
            DoSomething(bmp.Bitmap);
            bmp.Save();
        }

Upvotes: 8

MasterMastic
MasterMastic

Reputation: 21286

Perhaps you can do this with Metadata - but I'm not familiar with this subject so I'm not completely sure it's possible, nor do I how to do it.
What I do suggest is make a type.

So for example you can create a class which will have a Bitmap(1) property for the image, and a string for the file's path.

class LocalBitmap
{
    public Bitmap Bitmap { get; set; }
    public String Path { get; set; }

    public LocalBitmap(String path)
    {
        Path = path;
        Bitmap = new Bitmap(path);
    }
}

And use it like so:

LocalBitmapimage = new LocalBitmap(@"C:\myImage.bmp");


(1) Consider using the Image class instead. It's the base class of Bitmap. If you're not sure which one to use ask yourself if your class requires an image or an image of that exact format (that is, bitmap).

Upvotes: 6

TomTom
TomTom

Reputation: 62093

You do not. Simply. Basically the bitmap does not remember the file -it is your job.

YOu basically tesay "I get a picture send by UPS, after unpacking it - days later - how do I find the shipping number on the picture?" - you don not.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could use a variable:

var name = @"C:\<file name>";

and then:

using (Bitmap b = new Bitmap(name))
{
    ...
    b.Save(name);
}

Also notice that I have wrapped the Bitmap instance into a using statement in order to release unmanaged resources that are associated with it as soon as we are finished with it. You should always wrap IDisposable objects in using statements.

Upvotes: 5

Related Questions