473,473 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4629
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...
0
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,...
0
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...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.