473,221 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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

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
3 4618
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DotNetJunkies User | last post by:
i am using a dll that places an image data in memory and returns to me two wariable long P1 and long P2 i am using C# P1 is the memory address and P2 is the number of bytes written i want to place...
0
by: Anonieko Ramos | last post by:
> I have a graphics images that I want to convert to > ASCII art. How do I do it? > Code: - Default.aspx.cs
6
by: Saya | last post by:
Hello, This is a repost 'cause I haven't solve the problem: I can't use the System.Drawing class 'Image.FromStream' in the CompactFramework environment. What I've done with respect to Brendan's...
1
by: Mchuck | last post by:
I've seen several newsgroup topics everywhere concerning this, as well as a couple of articles from the MSDN website, but this error still baffles me. It has to do with using the...
3
by: CanyonJ | last post by:
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...
12
by: Lance | last post by:
hey all, first time vb.net 2005 user, after sticking vb6 out for a long time... anyway, using this code ====================== Dim FS As FileStream = File.OpenRead(Filename) Dim theImage As...
4
by: John Daly | last post by:
Does anyone know how to create an image (JPEG) from an HTTPResponse object? I am working on system that has a map pop up from our GIS department. I want create an image at runtime from this popup...
7
by: bookon | last post by:
I was running into the System.Drawing.Image.FromStream "parameter is not valid" on some of the images I was retrieving from a blob column in Sql Server. I thought there were corrupt images as...
10
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi, Please help me to write a dll in C# , that will read each pages of a tiff image from a file and a memory stream object ( need two ways) and creatre a new tiff image object.The dll should...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.