'Out of memory' / 'Parameter is not valid' at 'System.Drawing.Image' load

 Hi,

  i have a problem with loading images. what is interesting the same type of images, the same directories some of them are loading some - not. The image size doesn't matter. It happens on a big sized and on a small sized image. So..

 

  

int id = Int32.Parse(Request.Params["id"].ToString());
string sImage = "";


Response.Clear();
Response.ContentType = "image/JPEG";
Response.Expires = 0;
Response.Buffer = true;
Response.Clear();

sImage = GetMarkImagePath(id);

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

// the following line gives a 'Parameter is not valid' error System.Drawing.Image img = System.Drawing.Image.FromStream(fs, false, false);

// the following line will give a 'Out of memory' error
//System.Drawing.Image img = System.Drawing.Image.FromFile(sImage);
img.Save(Response.OutputStream, ImageFormat.Jpeg); Response.End(); img.Dispose(); fs.Close();

  
 and as i said, some images are loading fine , and some are not. The image type is for all the same - TIFF images. with 8 BitsPerPixel, Compression: None. The directory where the images are stored is a network place, the directory is not the problem.

I hope someone can help solve this out.

Thanks in advance 

0
S
6/25/2007 11:39:59 AM
asp.net.drawinggdi 1803 articles. 0 followers. Follow

20 Replies
2478 Views

Similar Articles

[PageSpeed] 15

system.drawing namespace is not supposed to be used in Web Applications.

 

http://msdn2.microsoft.com/en-us/library/system.drawing.aspx

 

It will eventually go out of memory because of high load, unless you use other methods, which I cannot think of right now.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/25/2007 12:05:44 PM

 Thanks for reply,

  how then to explain the thing that some images are loading well and some of them are not. If this was the answer the same image after a number of consequent requests would give an error but this doesn't happen...

0
S
6/25/2007 12:21:07 PM

One of the filestream object takes a buffer as an argument including the path and the access mode etc, I suggest trying that...

The other thing is checking the path that comes out of sImage by doing a response.write or alert or whatever to see it spits out correct path or running a debugger to see the instances.

If you come across some more info let us know..or put the whole code here...


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/25/2007 12:57:21 PM

it doesnt happen because that image is already loaded in the memory. The browser has already got the images.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/25/2007 12:58:24 PM

 thanks again... I tried several ways...

 

private System.Drawing.Image GetImage(string sFilePath)
{

FileStream fs = new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
System.Drawing.Image img = System.Drawing.Image.FromStream(fs, true, true);
fs.Close();

return img;
}

private System.Drawing.Image GetImageA(string sFilePath)
{

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

byte[] ImageArray = new byte[fs.Length];

byte[] b = new byte[2048];
int iLen = b.Length;
int iStart = 0;

try { while (fs.Read(b, 0, b.Length) > 0)
{
b.CopyTo(ImageArray, iStart);

if ((fs.Length - fs.Position) < b.Length)
{
while (fs.Position < fs.Length)
{
ImageArray.SetValue((byte)fs.ReadByte(), iStart++);
}
break;
}
iStart += b.Length;
}
}
catch (Exception ex)
{
Response.Write("Image load failed. (" + ex.Message + ")");
}

fs.Close();

MemoryStream ms = new MemoryStream(ImageArray, 0, ImageArray.Length);

return System.Drawing.Image.FromStream(ms, false);




}

Those 2 different functions from above returns the Image object and uses different approaches. The error is at the same line "System.Drawing.Image.FromStream(ms, false);". And when reloading the same image - The browser get refreshed each time, IE use cache (yes), but Mozilla FF - reloads well - so the image is reloaded again and again and here should pop up the error..
0
S
6/25/2007 1:14:56 PM

Check these two posts out while i check your code

http://blogs.msdn.com/omars/archive/2004/03/29/100941.aspx

http://www.glennjones.net/Post/799/Highqualitydynamicallyresizedimageswithnet.htm


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/25/2007 1:32:01 PM

S!ava:
System.Drawing.Image img = System.Drawing.Image.FromStream(fs, true, true);

The constructor takes boolean values only when you have specified embedded color managment and image data. Use this otherwise System.Drawing.Image.FromStream (Stream)


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/25/2007 1:35:15 PM

 Thanks naturehermit, but i think here is something to do with System.Drawing.Image class, i've been trying many options but none had the effect i expected , at least to load the image :) I read the articles at the url-s posted by you and found nothing new, the method System.Drawing.Image.FromStream (Stream stream) used with or without the boolean switches gives the same result. The second article uses in example the same method - also would give an error didn't even try it...

The funny thing - Images i use to load are scanned and saved with PhotoShop in .tif all the time. Some images are loaded by this class and some images aren't and i can assume that the file format has something to do with that and/or the class hasn't the format/error/or else  checking witch pops a error out. I took two images (one witch loads and the second is not) for testing, both saved with PhotoShop , having the same image properties. If the second image is saved with IrfanView using the same image format and format options does load, meaning the IrfanView fixes or converts the file to a readable by System.Drawing.Image class format.

The bad thing - Images must retain their original form, i cannot convert them in the source repository. The unique solution is to have a workaround the error and finally load those images. A conversion or something....

 

0
S
6/26/2007 5:19:02 AM

As I pointed out to you in my first reply that system.drawing is an unsafe class for use in web-apps. However is there a particular reason to use tiff format, because tiff is an unsupported format for web and as you already know its memory intensive too

Is there anything I can help with.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 8:07:11 AM

No, that's not right , at least in my case. Images witch are not loading in the web application - aren't loading in the Win32 Application either. So here the system.drawing seems to be an unsafe class at all :( .

Now i am trying to convert a image from a type to another TIF -> JPEG .. using byte arrays, and after that to send the Stream to ResposeOutput.


0
S
6/26/2007 8:35:25 AM

What application are you using to view the tiff files, May be the files are getting corrupted. You said you are using photoshop and photoshop produced .tiff should work fine, unless its .psd format


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 8:41:37 AM

naturehermit:

As I pointed out to you in my first reply that system.drawing is an unsafe class for use in web-apps.

This is the statement by Cheryl Simmons (MSFT) at the bottom of the link you provided concerning the caution “Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service”

“First of all, let me apologize for any confusion this statement has caused. It confused me when I was asked to add this statement to the documentation. The explanation I was given is that the System.Drawing.* classes were designed for use with Windows Forms and were tested for use with Windows Forms. They were not tested in services, which include ASP.NET applications. So when we say that System.Drawing.* classes are not supported for use in one of these applications, that is exactly what we mean. We are not saying they won’t work in an ASP.NET app or that we are aware of some bug that would cause you problems. What it does mean is that if you call Microsoft Product Support Services (PSS) because you are having a problem with System.Drawing.* class in your ASP application or service, they will not provide free support to you.”

You’re saying the namespace is ‘unsafe’, which is quite different. Where have you seen this information?


Paul Weston
0
bpw
6/26/2007 8:54:55 AM

In a normal way the images are loaded and viewed with a Delphi  app, this application opens images in a readonly mode - and the images are all shown fine, so here are no chance that files get corrupted. The images are not in psd format. I'm telling you that there must be something with the System.Drawing class. Why the images are opened well in a simple viewer but with this class there are errors..? it means that there is something wrong with the class itself.

I tried to send the raw stream to browser:  The IE shows garbage - it does not recognize the tiff MIME , but the MozillaFF using quicktime plugin shows the image.

I have to convert the images...from tiff to a JPEG , BMP or something ...

0
S
6/26/2007 9:02:09 AM

BPW, you have joined in the conversation now and if you read the second post, you will know what I mean. Not tested=Unpredicatable results=unsafe(safety not demonstrated). However if you want to wuss about it, I wont be a party to this. I am trying to help the guy and if you want to pick literals in my conversation, please continue to do so.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 9:03:27 AM

BPW as you have heard the guy say..there is something not right with the class and the msdn article points to that..so whether you agree or not its your choice, however to Slava..how do you intend to convert these into jpeg or whatever..

 

Do you want to do that in web app or in external app(local app). ?


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 9:07:59 AM

I want to convert them from the web application

0
S
6/26/2007 9:19:30 AM

Bitmap bm=Bitmap.FromFile("mypic.tiff");

save as jpeg...

bm.Save("mypig.jpg",ImageFormat.JPEG);

but again this uses system.drawing


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 9:35:28 AM

I’m using this namespace in my web app, so when I saw your statement I was concerned that you knew something I didn’t. I think the question I’m asking is reasonable and would be of interest to other readers of this thread. It wasn’t intended to cause offence, which it appears to have done, so I apologise.

Other then the link you pointed to, have you seen any other resources that address the following concerns I now have:

1) That the namespace is not supposed to be used in Web Applications
2) That “it will eventually go out of memory because of high load”
3) That system.drawing is an unsafe class for use in web-apps

Thank you


Paul Weston
0
bpw
6/26/2007 11:41:39 AM

BPW, if you have a questio

bpw:

 I think the question I’m asking is reasonable and would be of interest to other readers of this thread.

BPW, as you are aware this is Slava's thread and if you have a question, you need to ask it in a seperate thread. We are all friends here so you dont need to appologise but the trouble is it doesnt help slava whose thread it is.

bpw:

Other then the link you pointed to, have you seen any other resources that address the following concerns I now have:

1) That the namespace is not supposed to be used in Web Applications
2) That “it will eventually go out of memory because of high load”
3) That system.drawing is an unsafe class for use in web-apps

I think MSDN is an api for .net framework and If they write something over there...whether they mean it or not..we programmers take it that it is meant and tested to be so...

1. The link clearly says so that it should nt be used in web apps or windows service.

2. I do not know if I mentioned anywhere (something will go out of memory because of high load).

3. if you read what I wrote before which I shall write again for the sake of conveince. not tested, not recommended=unpredictable results=unsafe.

Please could I request that if this helps slava in some way we can continue discussing it here but if it doesnt and you feel that there is an issue with System.drawing namespace and I have somehow pointed to that, open a new thread where more knowledgeable people will be able to join and comment

 

Many thanks.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
6/26/2007 11:50:36 AM

 Was a resolution ever found for this? I'm having the exact problem with Photoshop/tif images in a windows app, I have IrfanView installed on my machine and have no problems displaying the images in the app, but when installed on other users computers they get the stupid 'Parameter is not valid' error. When I open the images in a text editor, it appears that the images with 'Adobe Photoshop CS2 Windows' in the header are the ones causing the errors, images with 'CS3' seem to display fine.

0
wardog
9/5/2008 4:22:11 PM
Reply:

Similar Artilces:

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

'do' won't 'do' if '/'
Greetings to All from Au, Have a NetWare Perl 5.8.4 and wanted to tweak File\Spec\NW.pm to try and standardise on '/' separators. If I run a test script (t/uni/lower.t) with an unmodified NW.pm, it calls t/uni/case.pl, that, in case.pl, (when the path separators are '\'), the $file is '..\lib\unicore\To\Lower.pl' and the following code portion works, with $simple getting a returned table: sub casetest { my ($base, $spec, $func) = @_; my $file = File::Spec->catfile(File::Spec->catdir(File::Spec->updir, "lib", &qu...

'''
Name: L Lachowsky Email: e2brutus_10atyahoodotcom Product: Firefox Summary: ''' Comments: why duddn this surprise me... I change default server from IE to Mozilla, and then i get error messages. well, shud I decide to not get on my computer with Mozilla..or shud I risk security breeches with IE....hmmm at least i can get on the internet with IE Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expectation of a response to this fee...

'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
In my aspx page, I have the following snippet: <%@ Page Language="C#" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Drawing" %>     public System.Drawing.Image Test()    {      ...    }  I keep getting the 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image' error in my Test function (which returns an Image). How do I fix this? Right which is possible; use the full...

CS0104: 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'
 CS0104: 'Image' is an ambiguous reference between 'System.Web.UI.WebControls.Image' and 'System.Drawing.Image'   <%@ Page Language="C#" %> <%@ Import Namespace="System.Drawing" %> <%@Import Namespace="System.Drawing.Imaging" %>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script type="text/C#" runat="server"> void Page_Load() {//The @ means "Ignore escape characters!" in the following string. It is a verbatim //literal string - any backslashes (and newlines) in...

'b'..'a'
Hello. I'm using defferent 5.6.0's for Win32. I wonder wheter following behaviour is intentional or not: d:\>perl -e "print 'b'..'c'" bc d:\>perl -e "print 'b'..'a'" bcdefghijklmnopqrstuvwxyz I expected empty list in latter case, like in perl -e "print 'bb'..'a'" <!ENTITY Vadim REALLIFE "Vadim V.Konovalov, St.Petersburg, Russia"> &Vadim; On Wed, Sep 20, 2000 at 03:14:41PM +0400, Konovalov, Vadim wrote: > Hello. > > I'm using defferent 5.6.0'...

'or' or 'union'
Hello I was just wondering, in general what is better to use, an 'or' clause in a select or a 'union' to join two selects together. Do both statements create work tables? Many thanks Alex I think OR will be better than union. because suppose u have 3 tables and using OR u can join table a and table b and table c so each table will have only one read. but using union you will join table a and table b and in another query of union u will use table (a or b) and table c so ur one read is more in union . Ramdas Alex Cheung wrote: > Hello > >...

'IN' Clause or 'OR'
Hello, [1]: select * from TABLEA where COL1 IN('value1','value2''valu3'....) [2]:select * from TABLEA where COL1 = 'value1' OR COL1= 'value2' OR COL1='valu3'. TABLEA is a huge table and it has non-clustered index on COL1. Among the above 2 queries, which query will give me the better performance or fast response and WHY? What is the difference between 'IN' and 'OR' clauses as for as Sybase Optimization is concerned. Which is the better one to be used on huge tables. Thanks. Mac An IN list is treated ...

'To', 'CC', & 'BCC'
Name: Dick Tracy Email: philipdottracyatoptusnetdotcomdotau Product: Thunderbird Summary: 'To', 'CC', & 'BCC' Comments: I have just started using Thunderbird and while I find it quite excellent- I suggest having a button to add addressees to 'BCC' as well. Currently each 'BCC' addressee has to be selected manually. I send e-mail to quite a number of people at a time and I do not wish to readily spread others addresses easily. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0....

'Value' should be between 'minimum' and 'maximum'.
I'm attempting to invoke with parameters:  Dim thing2 As New mydelsubPBStep(AddressOf pbStep) Me.Invoke(thing2, "setMax", dt.Rows.Count)      <---- this is where the error occurs   the delegate looks like this: Private Delegate Sub mydelsubPBStep(ByVal mode As String, ByVal value As Integer)   the function it's calling looks like this:Private Sub pbStep(ByVal mode As String, ByVal value As Integer) Select Case mode.ToLower Case "clear" ProgressBar1.Value = 0 Case "step" ProgressBar1.PerformStep() L...

EXEC sp_msforeachtable 'sp_spaceused ''?'''
What does the following SQL code mean?  EXEC sp_msforeachtable 'sp_spaceused ''?'''Johan TheunissenMCPD, MCSE, MCTS BizTalk 2006==============================Please mark the most helpful reply/replies as "Answer". JohanNL:sp_msforeachtable This is the name of the SP that resides in the master database.  This SP executes one or more commands for a table. JohanNL:sp_spaceused This is the command that you want to run for each of the tables residing in your current database.  As you might have understood so far, that this is again a...

Cannot convert value of parameter 'Course_FK' from 'System.String' to 'System.Guid'
Hi, I'm building a site and I've been trying to accomplish something similar to what is talked about in "Working with Data in ASP.NET 2.0 :: Declarative Parameters" tutorial. Below is the code for my methods from the xsd. I tried to attach a screenshot of my xsd but for some reason I'm not allowed GetAllCourseNames() SELECT        CourseID, NameFROM            Course GetCourseScheduleByCourseID SELECT        ScheduleID, Course_FK, Date, TeeTime, NumberOfPlayersF...

Web resources about - 'Out of memory' / 'Parameter is not valid' at 'System.Drawing.Image' load - asp.net.drawinggdi

Resources last updated: 12/2/2015 10:51:01 PM