从文件打开图像,然后释放锁定? [英] Open Image from file, then release lock?

查看:29
本文介绍了从文件打开图像,然后释放锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码行从文件中打开 Image:

I'm using the following line of code to open an Image from a file:

pictureBox1.Image = Image.FromFile("test.png");

我希望它锁定文件,将图像加载到内存中,将 pictureBox1.Image 设置为内存中的副本,然后释放锁定.实际上,直到我在内存中 Dispose()Image 之前,锁才会消失.在我清除内存中我正在使用的文件之前,我无法释放我不再使用的硬盘驱动器上文件的锁定.
微软的网站在一篇 C# 标签的文章中提到了它,但他们的解决方案是用 visual basic 编写的,这对我来说没用.

I expect it to lock the file, load the image to memory, set pictureBox1.Image to the copy in memory, and release the lock. In reality, the lock won't go away until I Dispose() of the Image in memory. I can not release the lock on the file on the harddrive that I am no longer using until I get rid of the file in memory that I am using.
Microsoft's site mentions it in a C#-labeled article, but their solution is written in visual basic, which is useless to me.

总结:我想将 pictureBox1.Image 设置为存储在 "test.png" 中的图像,然后让用户编辑或删除 "test.png"> 或者别的什么.

In summary: I want to set pictureBox1.Image to the image stored in "test.png", then let the user edit or delete "test.png" or whatever.

推荐答案

流的方法不正确.

请参阅此处https://stackoverflow.com/a/8701748/355264

以上链接中的正确代码:

Correct code from above link:

Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
    img = new Bitmap(bmpTemp);
}

这篇关于从文件打开图像,然后释放锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆