473,609 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image from file (release handle)

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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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
Jan 10 '08 #1
14 16318
According to reflector Image.FromStrea m() 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.Reference NotNull(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.Reference NotNull(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******** ******@TK2MSFTN GP03.phx.gbl...
>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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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

Jan 10 '08 #2
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.
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
Jan 10 '08 #3
This is what I'm using to read images w/o locking the file.

public static Image LoadImageFromFi le(string fileName)
{
Image theImage = null;
using (FileStream fileStream = new FileStream(file Name, FileMode.Open,
FileAccess.Read ))
{
byte[] img;
img = new byte[fileStream.Leng th];
fileStream.Read (img, 0, img.Length);
fileStream.Clos e();
theImage = Image.FromStrea m(new MemoryStream(im g));
img = null;
}
GC.Collect();
return theImage;
}
Hope it works for you.

RobinS.
GoldMail, Inc.
------------------------
"Steve K." <no***@nowhere. comwrote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
>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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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
Jan 10 '08 #4
On Thu, 10 Jan 2008 00:42:23 -0800, Steve K. <no***@nowhere. comwrote:
In a frantic solution-finding flurry I came up with a hack of sorts, I'm
still not sure what I think of it. Check it out:
That's essentially the same as copying the file into a MemoryStream,
except that it's even worse because it will decompress the file data into
memory. At least if you copy the file into a MemoryStream, whatever
compression is used in the TIFF (if any) still benefits you.

I'm glad you got a solution that works, just to prove it can be done. But
I don't think that's going to be the way you want to go, ultimately. :)

Pete
Jan 10 '08 #5
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
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.

Is it safe to assume that you hadn't seen my reply when you wrote that
post?
Hi Peter, yes, I sent this before seeing your reply.
Your post made a lot of sense, thank you again.
>
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

Jan 10 '08 #6
Hi Robin,

Thanks for the reply, that is what I've been doing as well for most other
image types. It's the TIFF that is the problem, as Peter pointed out the a
TIFF loaded into an Image still needs access to it's source stream.

Thanks,
Steve

"RobinS" <ro****@imnotte lling.comwrote in message
news:AP******** *************** *******@comcast .com...
This is what I'm using to read images w/o locking the file.

public static Image LoadImageFromFi le(string fileName)
{
Image theImage = null;
using (FileStream fileStream = new FileStream(file Name, FileMode.Open,
FileAccess.Read ))
{
byte[] img;
img = new byte[fileStream.Leng th];
fileStream.Read (img, 0, img.Length);
fileStream.Clos e();
theImage = Image.FromStrea m(new MemoryStream(im g));
img = null;
}
GC.Collect();
return theImage;
}
Hope it works for you.

RobinS.
GoldMail, Inc.
------------------------
"Steve K." <no***@nowhere. comwrote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
>>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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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

Jan 10 '08 #7
I've dealt with similar problems when working with Tiffs in particular, and
with GeoTiffs specifically, since that is what sort I work with primarily. I
ended up writing my own Tiff class, which initially only parses the Tiff
tags using a StreamReader, and creates a Collection of "ImageFileDirec tory"
instances, one for each image. The Image property of the ImageFileDirect ory
class reopens a StreamReader and gets the image out of the file. This is
also useful with Tiffs that are tiles rather than stripped, as I haven't
found a .Net class that can parse tiled Tiff format images, but my class can
work with either. Also, because these images are so large, not loading the
entire file saves a good bit of memory.

I was then able to create several derivatives of the class, a GeoTiff class
that recognizes the GeoTiff tags and can parse the Geographic information in
them, and a couple of derivatives of that for parsing several varieties of
GeoTiffs.

The Adobe Tiff file format is an open specification.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Steve K." <no***@nowhere. comwrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
Hi Robin,

Thanks for the reply, that is what I've been doing as well for most other
image types. It's the TIFF that is the problem, as Peter pointed out the
a TIFF loaded into an Image still needs access to it's source stream.

Thanks,
Steve

"RobinS" <ro****@imnotte lling.comwrote in message
news:AP******** *************** *******@comcast .com...
>This is what I'm using to read images w/o locking the file.

public static Image LoadImageFromFi le(string fileName)
{
Image theImage = null;
using (FileStream fileStream = new FileStream(file Name, FileMode.Open,
FileAccess.Rea d))
{
byte[] img;
img = new byte[fileStream.Leng th];
fileStream.Read (img, 0, img.Length);
fileStream.Clos e();
theImage = Image.FromStrea m(new MemoryStream(im g));
img = null;
}
GC.Collect();
return theImage;
}
Hope it works for you.

RobinS.
GoldMail, Inc.
------------------------
"Steve K." <no***@nowhere. comwrote in message
news:u4******* *******@TK2MSFT NGP03.phx.gbl.. .
>>>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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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
SetActiveFram e() 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


Jan 10 '08 #8


"Steve K." <no***@nowhere. comwrote in message
news:u4******** ******@TK2MSFTN GP03.phx.gbl...
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 ImageFromFileRe leaseHandle(str ing filename)
{
FileStream fs = null;
try
{
fs = new FileStream(file name, FileMode.Open, FileAccess.Read );
return Image.FromStrea m(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
You stated in another post that your image data is coming directly from a
database blob field. Why don't you just take this data and write it to a
MemoryStream and then do a FromStream method call to create the image? This
way, you don't have to create/delete a file on the file system.

HTH,
Mythran
Jan 10 '08 #9
On Thu, 10 Jan 2008 01:45:51 -0800, Steve K. <no***@nowhere. comwrote:
Thanks for the reply, that is what I've been doing as well for most other
image types. It's the TIFF that is the problem, as Peter pointed out
the a
TIFF loaded into an Image still needs access to it's source stream.
Actually, the code Robin posted is exactly my second suggestion: it reads
the file entirely into a MemoryStream and uses that instead of the
original file as the source for the image.

It should work fine as long as the files aren't too big.

Pete
Jan 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4555
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
0
1795
by: Jean-Sebastien Carle | last post by:
I've been ripping my hair our trying to get this to work for a week and I can't seem to get it. I'm trying to display a 4BppIndexed Bitmap from within the ntoskrnl.exe file that contains no palette. This bitmap is the one everyone likes to change to customize their boot screen. My goal is not to edit the kernel, but simply to display the bitmap content in much the same way a resource editor does. My problem is that no matter what I do, the...
0
4128
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving as a image it uses image.Save method() This image.Save function have two overloaded form's first one is file name
35
2655
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
15
5326
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
0
11135
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct STRUCT_RECT
0
2349
by: bindslind | last post by:
The script does most of what it's supposed to do, but I have what seems to be a simple problem that I can't figure out. When the page intitially loads the first page does not display images. The same things happens when the "1" link is selected and ofcourse when "previous" is clicked from page 2. Other than this everything is a-ok. I think I just may need another set of eyes to check it out. Thanks. And here's the code: <?php ...
6
2223
by: googletired | last post by:
Hello, I haven't made a XSL in quite sometime so i am very rusty. Basicly i want the XSL to display a defined image if one is not present in the XML. here is my current XSL and XML will be below that, hope you can help. <?xml version='1.0' encoding='utf-8' ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" encoding="ISO-8859-1" omit-xml-declaration="yes"/>
6
1984
by: Thomas Guettler | last post by:
Hi, I tried PIL for image batch processing. But somehow I don't like it - Font-Selection: You need to give the name of the font file. - Drawing on an image needs a different object that pasting and saving. - The handbook is from Dec. 2006. What image libraries do you suggest? I think there are these alternatives:
0
8112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8047
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8510
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8376
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.