By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Image.FromStream() works for a while, then breaks

P: n/a
I've been running into a frustrating problem for a while now. I use
image.FromStream to open a tif, then perform some drawstring methods, and
then save the file again. The tiffs are in 1 bit per pixel format, so there
is some compression going on when we save the file. Quite a few files are
opened and closed. Everything seems to work fine for a couple hours. The
only draw back is the cpu is generally at 100%. Then the code starts failing
at the FromStream method call with the "Object reference not set to an
instance of an object."
The tif resides on the machine's hard drive and looks fine. The file stream
also looks fine. I can close the process and reopen and the tiff that just
failed will now succeed. I am relying on the stream.close and the image
object to go out of scope to close the image object. I'm not calling
dispose. Maybe this is the cause of the problem problem, the machine
eventually gets in a bad state. Any help would be greatly appreciated.

using( Stream stream = File.Open( sourceFile, FileMode.Open,
FileAccess.Read, FileShare.Read ) )
{
if( stream == null )
{
throw new Exception( ... );
}

try
{
// Don't use fromFile, it puts a lock on the file and it stays locked
// long after the imageObject goes out of scope.
image = System.Drawing.Image.FromStream( stream );
}
catch( Exception ex ){...}

image.drawstring(...)
save image with 1bit-per-pixel compression
}

Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Thanks for the reply. My problem occurs on the FromStream call, so I
wouldn't even make it to the rotate methods. I don't think your problem is
quite the same, but I think your fix might help me elsewhere.

On another note, I've since added the Image.Dispose call to my code with no
success. I'm still running into the error on FromStream. The only extra
clue I have now is that occasionally I get an error from the dispose method
that says "This object is use elsewhere". What I don't know yet is if this
error is occuring at the same time I get the FromStream error, or 1 file
before.

"mattaku" wrote:
Not sure if my problem is related. I've been trying to read in Image data
from a memory stream after converting it from base64 text. I can read in an
Image and set it as the Form1.backgroundImage, but could not read in the
base64 text data. This does work for other Images, but not backgroundImage
properties. Well, I read some other threads elsewhere and noticed that
someone fixed some other "FromStream" method bug by rotating the Image.
Well, sure enough, my code started to work! Don't ask me why.

byte[] byteArray = Convert.FromBase64String( base64String );
MemoryStream memStrm = new MemoryStream( byteArray, 0, byteArray.Length );
retObj = Image.FromStream( memStrm );

// these next two lines is to fix a weird C#/.Net bug
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
memStrm.Close();


"CanyonJ" wrote:
I've been running into a frustrating problem for a while now. I use
image.FromStream to open a tif, then perform some drawstring methods, and
then save the file again. The tiffs are in 1 bit per pixel format, so there
is some compression going on when we save the file. Quite a few files are
opened and closed. Everything seems to work fine for a couple hours. The
only draw back is the cpu is generally at 100%. Then the code starts failing
at the FromStream method call with the "Object reference not set to an
instance of an object."
The tif resides on the machine's hard drive and looks fine. The file stream
also looks fine. I can close the process and reopen and the tiff that just
failed will now succeed. I am relying on the stream.close and the image
object to go out of scope to close the image object. I'm not calling
dispose. Maybe this is the cause of the problem problem, the machine
eventually gets in a bad state. Any help would be greatly appreciated.

using( Stream stream = File.Open( sourceFile, FileMode.Open,
FileAccess.Read, FileShare.Read ) )
{
if( stream == null )
{
throw new Exception( ... );
}

try
{
// Don't use fromFile, it puts a lock on the file and it stays locked
// long after the imageObject goes out of scope.
image = System.Drawing.Image.FromStream( stream );
}
catch( Exception ex ){...}

image.drawstring(...)
save image with 1bit-per-pixel compression
}

Jul 21 '05 #2

P: n/a
I don't need to rotate my image either...

The solution is to just rotate the image 180 degrees then back 180 degrees.
This "fixes" something wrong with the Image object. Try it.
"CanyonJ" wrote:
Thanks for the reply. My problem occurs on the FromStream call, so I
wouldn't even make it to the rotate methods. I don't think your problem is
quite the same, but I think your fix might help me elsewhere.

On another note, I've since added the Image.Dispose call to my code with no
success. I'm still running into the error on FromStream. The only extra
clue I have now is that occasionally I get an error from the dispose method
that says "This object is use elsewhere". What I don't know yet is if this
error is occuring at the same time I get the FromStream error, or 1 file
before.

"mattaku" wrote:
Not sure if my problem is related. I've been trying to read in Image data
from a memory stream after converting it from base64 text. I can read in an
Image and set it as the Form1.backgroundImage, but could not read in the
base64 text data. This does work for other Images, but not backgroundImage
properties. Well, I read some other threads elsewhere and noticed that
someone fixed some other "FromStream" method bug by rotating the Image.
Well, sure enough, my code started to work! Don't ask me why.

byte[] byteArray = Convert.FromBase64String( base64String );
MemoryStream memStrm = new MemoryStream( byteArray, 0, byteArray.Length );
retObj = Image.FromStream( memStrm );

// these next two lines is to fix a weird C#/.Net bug
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
memStrm.Close();


"CanyonJ" wrote:
I've been running into a frustrating problem for a while now. I use
image.FromStream to open a tif, then perform some drawstring methods, and
then save the file again. The tiffs are in 1 bit per pixel format, so there
is some compression going on when we save the file. Quite a few files are
opened and closed. Everything seems to work fine for a couple hours. The
only draw back is the cpu is generally at 100%. Then the code starts failing
at the FromStream method call with the "Object reference not set to an
instance of an object."
The tif resides on the machine's hard drive and looks fine. The file stream
also looks fine. I can close the process and reopen and the tiff that just
failed will now succeed. I am relying on the stream.close and the image
object to go out of scope to close the image object. I'm not calling
dispose. Maybe this is the cause of the problem problem, the machine
eventually gets in a bad state. Any help would be greatly appreciated.

using( Stream stream = File.Open( sourceFile, FileMode.Open,
FileAccess.Read, FileShare.Read ) )
{
if( stream == null )
{
throw new Exception( ... );
}

try
{
// Don't use fromFile, it puts a lock on the file and it stays locked
// long after the imageObject goes out of scope.
image = System.Drawing.Image.FromStream( stream );
}
catch( Exception ex ){...}

image.drawstring(...)
save image with 1bit-per-pixel compression
}

Jul 21 '05 #3

P: n/a
Sorry, I didn't read your response that closely. Ignore previous response.
:) Can you try to read in the file into a buffer and create a memoryStream
(just to see if that works)?
"mattaku" wrote:
I don't need to rotate my image either...

The solution is to just rotate the image 180 degrees then back 180 degrees.
This "fixes" something wrong with the Image object. Try it.
"CanyonJ" wrote:
Thanks for the reply. My problem occurs on the FromStream call, so I
wouldn't even make it to the rotate methods. I don't think your problem is
quite the same, but I think your fix might help me elsewhere.

On another note, I've since added the Image.Dispose call to my code with no
success. I'm still running into the error on FromStream. The only extra
clue I have now is that occasionally I get an error from the dispose method
that says "This object is use elsewhere". What I don't know yet is if this
error is occuring at the same time I get the FromStream error, or 1 file
before.

"mattaku" wrote:
Not sure if my problem is related. I've been trying to read in Image data
from a memory stream after converting it from base64 text. I can read in an
Image and set it as the Form1.backgroundImage, but could not read in the
base64 text data. This does work for other Images, but not backgroundImage
properties. Well, I read some other threads elsewhere and noticed that
someone fixed some other "FromStream" method bug by rotating the Image.
Well, sure enough, my code started to work! Don't ask me why.

byte[] byteArray = Convert.FromBase64String( base64String );
MemoryStream memStrm = new MemoryStream( byteArray, 0, byteArray.Length );
retObj = Image.FromStream( memStrm );

// these next two lines is to fix a weird C#/.Net bug
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
((Image)retObj).RotateFlip( RotateFlipType.Rotate180FlipNone );
memStrm.Close();


"CanyonJ" wrote:

> I've been running into a frustrating problem for a while now. I use
> image.FromStream to open a tif, then perform some drawstring methods, and
> then save the file again. The tiffs are in 1 bit per pixel format, so there
> is some compression going on when we save the file. Quite a few files are
> opened and closed. Everything seems to work fine for a couple hours. The
> only draw back is the cpu is generally at 100%. Then the code starts failing
> at the FromStream method call with the "Object reference not set to an
> instance of an object."
> The tif resides on the machine's hard drive and looks fine. The file stream
> also looks fine. I can close the process and reopen and the tiff that just
> failed will now succeed. I am relying on the stream.close and the image
> object to go out of scope to close the image object. I'm not calling
> dispose. Maybe this is the cause of the problem problem, the machine
> eventually gets in a bad state. Any help would be greatly appreciated.
>
> using( Stream stream = File.Open( sourceFile, FileMode.Open,
> FileAccess.Read, FileShare.Read ) )
> {
> if( stream == null )
> {
> throw new Exception( ... );
> }
>
> try
> {
> // Don't use fromFile, it puts a lock on the file and it stays locked
> // long after the imageObject goes out of scope.
> image = System.Drawing.Image.FromStream( stream );
> }
> catch( Exception ex ){...}
>
> image.drawstring(...)
> save image with 1bit-per-pixel compression
> }
>

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.