来自文件的图像(释放句柄) [英] Image from file (release handle)

查看:56
本文介绍了来自文件的图像(释放句柄)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,我用来从一个文件中获取System.Drawing.Image而没有

保持文件打开的句柄(所以我可以删除文件)。这是

代码:


< code>

public static Image ImageFromFileReleaseHandle(string filename)

{

FileStream fs = null;

尝试

{

fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

返回Image.FromStream(fs);

}

终于

{

fs.Close();

}

}

< / code>


多年来一直很好......直到我尝试加载多帧(页面)

tiff。当使用上面的方法加载tif时,我无法调用

SetActiveFrame()到0以外的任何帧索引。当我这样做时,它会抛出

dreaded Generic GDI + Error ...


如果我使用System.Drawing.FromFile()我可以使用TIF没问题 -

问题我不能删除源文件。


所以我想知道的是Image.FromFile()与我的不同之处是什么? b $ b在做什么?有谁知道什么,如果有什么方法可以做什么

特别?


任何帮助非常感谢。


-Steve

I have a method that I use to get a System.Drawing.Image from a file without
keeping a handle on the file open (so I can delete the file). Here is the
code:

<code>
public static Image ImageFromFileReleaseHandle(string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
return Image.FromStream(fs);
}
finally
{
fs.Close();
}
}
</code>

It''s worked great for years... until I tried loading a multi-frame(page)
tiff. When loading a tif using the above method I''m unable to call
SetActiveFrame() to any frame index other than 0. When I do, it throws the
dreaded "Generic GDI+ Error..."

If I use System.Drawing.FromFile() I can work with the TIF no problem -
problem is I can''t delete the source file.

So what I''m wondering is how is Image.FromFile() different than what I''m
doing? Does anyone know what, if anything that method is doing that is
special?

Any help greatly appreciated.

-Steve

推荐答案

根据反射器Image.FromStream()和Image.FromFile()都是

来电相同的内部GDI方法。嗯,不一样,但他们是GDI

方法(我认为)这很难解释。我说的是他们俩都是b $ b看起来他们的工作方式是一样的。


Sheesh!


所以我有了这个好主意!

< code>

Image i = Bitmap.FromFile(filename);

Guard。 ReferenceNotNull(i,无法从文件加载图像:&+; + filename);


图像j = i.Clone()为图像;

i .Dispose();

返回j;

< / code>


它不允许我删除该文件文件锁定另一个

进程....

如果我也处理j,那么我可以删除该文件。所以看来

close仍然保留对源对象的一些引用?这听起来不是很好。


最后:

< code>

Image i = Bitmap.FromFile(filename);

Guard.ReferenceNotNull(i,无法从文件加载图片:&+; + filename);


//尝试将一个绘制到另一个中

图像freeCopy =新位图(i);

i.Dispose();

返回freeCopy ;

< / code>


这会丢弃tiff中的所有帧。我可以删除源文件,但是

结果图像是垃圾。


我唯一的最后一个选项是加载tiff,抓取所有帧进入一个

Image [],处理源tiff图像,删除源文件,然后使用Image []数组工作
。这将适合我的需求,但我想是

其他它不会。


如果有人有任何想法或建议我真的很想听到他们的声音。


-Steve

" Steve K." < no *** @ nowhere.com写了留言

news:u4 ************** @ TK2MSFTNGP03.phx.gbl ...
According to reflector Image.FromStream() and Image.FromFile() are both
calling the same internal GDI methods. Well, not the same but they ARE GDI
methods (I think) It''s hard to explain. What I''m saying is that they both
look like they work the same way.

Sheesh!

So then I had this bright idea!
<code>
Image i = Bitmap.FromFile(filename);
Guard.ReferenceNotNull(i, "Failed loading Image from file: " + filename);

Image j = i.Clone() as Image;
i.Dispose();
return j;
</code>

It doesn''t allow me to delete the file either "File locked by another
process...."
if I dispose j as well, then I can delete the file. So it appears that
close still keep some reference to the source object? That doesn''t sound
right.

And finally:
<code>
Image i = Bitmap.FromFile(filename);
Guard.ReferenceNotNull(i, "Failed loading Image from file: " + filename);

// try painting one into the other
Image freeCopy = new Bitmap(i);
i.Dispose();
return freeCopy;
</code>

This drops all the frames from the tiff. I can delete the source file, but
th resultant Image is junk.

The only last option I have is to load the tiff, grab all the frames into an
Image[], dispose of the source tiff Image, delete the source file and just
work with the Image[] array. This will work for my needs, but I imagine for
other it won''t.

If anyone has any ideas or suggestion I would really like to hear them.

-Steve
"Steve K." <no***@nowhere.comwrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...

>我有一个方法用于从文件中获取System.Drawing.Image
而不保持文件打开的句柄(所以我可以删除文件)。这里
是代码:


< code>

public static Image ImageFromFileReleaseHandle(string filename)

{

FileStream fs = null;

尝试

{

fs = new FileStream(filename,FileMode.Open, FileAccess.Read);

返回Image.FromStream(fs);

}

终于

{

fs.Close();

}

}

< / code>

多年来一直很好......直到我尝试加载一个多帧(页面)

tiff。当使用上面的方法加载tif时,我无法调用

SetActiveFrame()到0以外的任何帧索引。当我这样做时,它会抛出可怕的
这个可怕的Generic GDI + Error ...


如果我使用System.Drawing.FromFile()我可以使用TIF没问题 -

问题我不能删除源文件。


所以我想知道的是Image.FromFile()与我的不同之处是什么? b $ b在做什么?有谁知道什么,如果有什么方法可以做什么

特别?


任何帮助非常感谢。


-Steve
>I have a method that I use to get a System.Drawing.Image from a file
without keeping a handle on the file open (so I can delete the file). Here
is the code:

<code>
public static Image ImageFromFileReleaseHandle(string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
return Image.FromStream(fs);
}
finally
{
fs.Close();
}
}
</code>

It''s worked great for years... until I tried loading a multi-frame(page)
tiff. When loading a tif using the above method I''m unable to call
SetActiveFrame() to any frame index other than 0. When I do, it throws
the dreaded "Generic GDI+ Error..."

If I use System.Drawing.FromFile() I can work with the TIF no problem -
problem is I can''t delete the source file.

So what I''m wondering is how is Image.FromFile() different than what I''m
doing? Does anyone know what, if anything that method is doing that is
special?

Any help greatly appreciated.

-Steve



2008年1月10日星期四00:22:05 -0800,Steve K.< no *** @ nowhere.comwrote:
On Thu, 10 Jan 2008 00:22:05 -0800, Steve K. <no***@nowhere.comwrote:

[...]

如果有人有任何想法或建议我真的很想听。
[...]
If anyone has any ideas or suggestion I would really like to hear them.



当您写下

帖子时,可以安全地假设您没有看到我的回复吗?


你曾经尝试过的所有事情看起来都很糟糕,对我来说并不令人惊讶。对象类隐含地读取整个TIFF中的

是没有意义的,即使你克隆它也是如此。实现这种一般行为的效率太低了。


希望我之前发表的文章有所帮助。


Pete

Is it safe to assume that you hadn''t seen my reply when you wrote that
post?

The behaviors you''ve seen with the things you''ve tried all seem
unsurprising to me. It wouldn''t make sense for the object class to read
in the entire TIFF implicitly, even when you clone it. It''s just too
inefficient to make that the general behavior.

Hopefully something in my previous post helps.

Pete


这是我用来读取没有锁定文件的图像。


public static Image LoadImageFromFile(string fileName)

{

Image theImage = null;

using(FileStream fileStream = new FileStream(fileName,FileMode。打开,

FileAccess.Read))

{

byte [] img;

img = new byte [ fileStream.Length];

fileStream.Read(img,0,img.Length);

fileStream.Close();

theImage = Image.FromStream(new MemoryStream(img));

img = null;

}

GC.Collect();

返回图片;

}

希望它适合你。


RobinS。

GoldMail,Inc。

------------------------

" Steve K " < no *** @ nowhere.com写了留言

news:u4 ************** @ TK2MSFTNGP03.phx.gbl ...
This is what I''m using to read images w/o locking the file.

public static Image LoadImageFromFile(string fileName)
{
Image theImage = null;
using (FileStream fileStream = new FileStream(fileName, FileMode.Open,
FileAccess.Read))
{
byte[] img;
img = new byte[fileStream.Length];
fileStream.Read(img, 0, img.Length);
fileStream.Close();
theImage = Image.FromStream(new MemoryStream(img));
img = null;
}
GC.Collect();
return theImage;
}
Hope it works for you.

RobinS.
GoldMail, Inc.
------------------------
"Steve K." <no***@nowhere.comwrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...

>我有一个方法用于从文件中获取System.Drawing.Image
而不保持文件打开的句柄(所以我可以删除文件)。这里
是代码:


< code>

public static Image ImageFromFileReleaseHandle(string filename)

{

FileStream fs = null;

尝试

{

fs = new FileStream(filename,FileMode.Open, FileAccess.Read);

返回Image.FromStream(fs);

}

终于

{

fs.Close();

}

}

< / code>

多年来一直很好......直到我尝试加载一个多帧(页面)

tiff。当使用上面的方法加载tif时,我无法调用

SetActiveFrame()到0以外的任何帧索引。当我这样做时,它会抛出可怕的
这个可怕的Generic GDI + Error ...


如果我使用System.Drawing.FromFile()我可以使用TIF没问题 -

问题我不能删除源文件。


所以我想知道的是Image.FromFile()与我的不同之处是什么? b $ b在做什么?有谁知道什么,如果有什么方法可以做什么

特别?


任何帮助非常感谢。


-Steve
>I have a method that I use to get a System.Drawing.Image from a file
without keeping a handle on the file open (so I can delete the file). Here
is the code:

<code>
public static Image ImageFromFileReleaseHandle(string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
return Image.FromStream(fs);
}
finally
{
fs.Close();
}
}
</code>

It''s worked great for years... until I tried loading a multi-frame(page)
tiff. When loading a tif using the above method I''m unable to call
SetActiveFrame() to any frame index other than 0. When I do, it throws
the dreaded "Generic GDI+ Error..."

If I use System.Drawing.FromFile() I can work with the TIF no problem -
problem is I can''t delete the source file.

So what I''m wondering is how is Image.FromFile() different than what I''m
doing? Does anyone know what, if anything that method is doing that is
special?

Any help greatly appreciated.

-Steve


这篇关于来自文件的图像(释放句柄)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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