当我从文件中获取图像时,文件保持锁定状态 [英] When I get the image from the file the file remains locked

查看:50
本文介绍了当我从文件中获取图像时,文件保持锁定状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从文件中获取图像时,文件仍然处于锁定状态,因此删除

失败并显示另一个进程使用


所以我尝试使用克隆并处理获得的图像。


但这并没有解决问题。


你能帮忙吗?


Dim zz As Image = Image.FromFile(imageFileName)


MyImage = zz.Clone


zz.Dispose()


zz =没什么


File.Delete(imageFileName)

解决方案



这里克隆方法的问题是它还复制了IMAGE对象中加载的文件的引用

所以如果您在克隆后将zz对象处理为
,它会将文件的引用传递给MyImage

对象,这就是文件最终保持锁定的原因。

试试这段代码。这应该可以解决你的问题


Dim fs As New StreamReader(" C:\ all.jpg")

Dim img As Image = Image.FromStream (fs.BaseStream)

fs.Close()

PictureBox1.Image = img

File.Delete(" C:\ all .jpg")

幸运




我真的很想知道这是不是作品。 Windows中的柯达Image驱动程序

98和NT4有同样的问题。我一直认为这是柯达驱动程序中的一个错误,但如果它存在于.NET中(也没有使用柯达

驱动程序),那么这个问题实际上可能存在于底层的Win32

API中。


Mike Ober。


" Lucky" <涂************ @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

这里克隆方法的问题是它还复制了加载在IMAGE对象中的文件的引用,以便在克隆后处理zz对象,它将文件的引用传递给MyImage
对象,这就是文件最终保持锁定的原因。
尝试此代码。这应该可以解决你的问题

Dim fs As New StreamReader(" C:\ all.jpg")
Dim img As Image = Image.FromStream(fs.BaseStream)
fs.Close()
PictureBox1.Image = img
File.Delete(" C:\ all.jpg")

Lucky



首先,您需要了解该类的行为以及它处理资源的方式。
>

在你的情况下IMAGE类锁定资源,这样如果对象需要参加刷新/重装需要参加的话比原始数据要好

由对象本身重新加载或修改。


i不知道为什么它在IMAGE类中是这样的,但MS已经表示

只有这样才。


运行我的代码并将其与你的代码进行比较,你会看到差异

和类的行为
< br $> b $ b幸运


When I get the image from the file the file remains locked so the Delete
fails with a "used by another process"

So I tried using a clone and disposing the obtained image.

But that didn''t fix the problem.

Can you help?

Dim zz As Image = Image.FromFile(imageFileName)

MyImage = zz.Clone

zz.Dispose()

zz = Nothing

File.Delete(imageFileName)

解决方案

hi,
the problem here with cloning method is it also copies the reference
of the file that is loaded in the IMAGE object so that if you dispose
the zz object after cloning, it passes reference of the file to MyImage
object that''s why ultimately the file remains locked.

try this code. this should solve your problem

Dim fs As New StreamReader("C:\all.jpg")
Dim img As Image = Image.FromStream(fs.BaseStream)
fs.Close()
PictureBox1.Image = img
File.Delete("C:\all.jpg")
Lucky



I''d really like to know if this works. The Kodak Image drivers in Windows
98 and NT4 have the same problem. I always thought this was a bug in the
Kodak drivers, but if it exists in .NET (which doesn''t use the Kodak
drivers) as well, then the problem may actually be in the underlying Win32
API.

Mike Ober.

"Lucky" <tu************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

hi,
the problem here with cloning method is it also copies the reference
of the file that is loaded in the IMAGE object so that if you dispose
the zz object after cloning, it passes reference of the file to MyImage
object that''s why ultimately the file remains locked.

try this code. this should solve your problem

Dim fs As New StreamReader("C:\all.jpg")
Dim img As Image = Image.FromStream(fs.BaseStream)
fs.Close()
PictureBox1.Image = img
File.Delete("C:\all.jpg")
Lucky




well first you need to understand the behaviour of the class and the
way it treats the resources.

in your case IMAGE class lock the resources so that if any call for
refresh/reload needs to attend by the object than the original data can
be reloaded or modified by the object itself.

i dont know why it is like this in IMAGE class but MS has implimented
it this way only.

run my code and compare it with your''s and you''ll see the difference
and behaviour of the class

Lucky


这篇关于当我从文件中获取图像时,文件保持锁定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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