如何从文件加载图像而不保持文件锁定?

发布于 2024-11-06 05:47:25 字数 494 浏览 0 评论 0原文

我希望在 PictureBox 中显示图像,从文件加载图像。不过,该文件会定期被覆盖,因此我无法保持文件锁定。我首先这样做:

pictureBox.Image = Image.FromFile( fileName );

但是,这会使文件保持锁定状态。然后我尝试读取一个流:

using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    pictureBox.Image = Image.FromStream(fs);
} 

这不会锁定文件,但确实会导致稍后抛出异常; MSDN 指出流必须在图像的生命周期内保持打开状态。 (例外情况包括“可能无法读取已关闭的文件”或类似消息。)

如何从文件加载图像,然后不再引用该文件?

I wish to display an image in a PictureBox, loading the image from a file. The file gets overwritten periodically though, so I can't keep the file locked. I started by doing this:

pictureBox.Image = Image.FromFile( fileName );

However, this keeps the file locked. Then I tried to read through a stream:

using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    pictureBox.Image = Image.FromStream(fs);
} 

This doesn't lock the file, but does cause an exception to be thrown later on; MSDN indicates that the stream must be kept open for the lifetime of the image. (The exception includes a message that "A closed file may not be read" or similar.)

How can I load an image from a file, then have no further references to the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街道布景 2024-11-13 05:47:25

很抱歉回答我自己的问题,但我认为这太有用了,不适合自己保留。

诀窍是在将数据加载到图像中之前将数据从文件流复制到内存流中。然后可以安全地关闭文件流。

using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    fs.CopyTo(ms);
    ms.Seek(0, System.IO.SeekOrigin.Begin);
    pictureBox.Image = Image.FromStream(ms);
} 

Sorry to answer my own question, but I thought this was too useful to keep to myself.

The trick is to copy the data from the file stream into a memory stream before loading it into an image. Then the file stream may be closed safely.

using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    fs.CopyTo(ms);
    ms.Seek(0, System.IO.SeekOrigin.Begin);
    pictureBox.Image = Image.FromStream(ms);
} 
心奴独伤 2024-11-13 05:47:25

对于那些在 Framework 4.0 以下工作的人,这就是我所做的:

Using fs As New System.IO.FileStream(cImage, IO.FileMode.Open, IO.FileAccess.Read)
            Dim buffer(fs.Length) As Byte
            fs.Read(buffer, 0, fs.Length - 1)
            Using ms As New System.IO.MemoryStream
                ms.Write(buffer, 0, buffer.Length - 1)
                picID.Image = Image.FromStream(ms)
            End Using
        End Using

For those working below Framework 4.0, here's what I did:

Using fs As New System.IO.FileStream(cImage, IO.FileMode.Open, IO.FileAccess.Read)
            Dim buffer(fs.Length) As Byte
            fs.Read(buffer, 0, fs.Length - 1)
            Using ms As New System.IO.MemoryStream
                ms.Write(buffer, 0, buffer.Length - 1)
                picID.Image = Image.FromStream(ms)
            End Using
        End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文