获取位图文件名

问题描述 投票:7回答:6

我使用下面的这一行来创建我的Bitmap:

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

修改此位图后,我想将其保存在下面的行中:

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

我的问题是 - 如何从属性中获取文件名位图。 简单地说,我需要保存位图,并使用与我发起它的名称相同的位图。

谢谢

c# bitmap save filenames
6个回答
6
投票

也许你可以用Metadata做到这一点 - 但我不熟悉这个主题所以我不完全确定它是可能的,我也不知道怎么做。 我的建议是做一个类型。

因此,例如,您可以创建一个类,该类具有图像的Bitmap(1)属性,以及文件路径的字符串。

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

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

并像这样使用它:

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).

8
投票

得到满足的答案会让你陷入困境。从文件加载位图会锁定文件。如果不首先处理位图,则无法将其保存。这是一个鸡和蛋的问题,如果不克隆位图就无法解决。这本身有点棘手,Bitmap.Clone()方法是一个优化版本,它使用相同的内存部分,首先将锁定放在文件上。所以实际上并没有释放锁。

这是一个小类,负责创建深度克隆并记忆原始位图的路径和格式:

    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;
    }

并像这样使用它:

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

5
投票

你可以使用一个变量:

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

然后:

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

另请注意,我已将Bitmap实例包装到using语句中,以便在我们完成它后立即释放与其关联的非托管资源。你应该总是在IDisposable语句中包装using对象。


1
投票

可能的解决方法是使用Bitmap的Tag属性在创建时存储路径。

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

..然后稍后检索路径..

string pathFromBitmap = (string)myBitmap.Tag;

..just确保将Tag强制转换为字符串,并将Tag复制到源位图制作的任何实例。


0
投票

你不。只是。基本上位图不记得文件 - 这是你的工作。

你基本上都在测试“我得到一张UPS发送的图片,打开包装后 - 几天后 - 如何在图片上找到发货号码?” - 你不能。


0
投票

另一种解决锁定汉斯描述的文件问题的方法是freeze图像。

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; }
}
© www.soinside.com 2019 - 2024. All rights reserved.