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

  • Question

  • User579761722 posted

     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 

    Monday, June 25, 2007 7:39 AM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2007 9:32 AM
  • User478895623 posted

    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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 26, 2007 4:54 AM

All replies

  • User-1091210821 posted

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

     

    http://msdn.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.

    Monday, June 25, 2007 8:05 AM
  • User579761722 posted

     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...

    Monday, June 25, 2007 8:21 AM
  • User-1091210821 posted

    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...

    Monday, June 25, 2007 8:57 AM
  • User-1091210821 posted

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

    Monday, June 25, 2007 8:58 AM
  • User579761722 posted

     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..
    Monday, June 25, 2007 9:14 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2007 9:32 AM
  • User-1091210821 posted

    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)

    Monday, June 25, 2007 9:35 AM
  • User579761722 posted

     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....

     

    Tuesday, June 26, 2007 1:19 AM
  • User-1091210821 posted

    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.

    Tuesday, June 26, 2007 4:07 AM
  • User579761722 posted

    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.


    Tuesday, June 26, 2007 4:35 AM
  • User-1091210821 posted

    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

    Tuesday, June 26, 2007 4:41 AM
  • User478895623 posted

    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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 26, 2007 4:54 AM
  • User579761722 posted

    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 ...

    Tuesday, June 26, 2007 5:02 AM
  • User-1091210821 posted

    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.

    Tuesday, June 26, 2007 5:03 AM
  • User-1091210821 posted

    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). ?

    Tuesday, June 26, 2007 5:07 AM
  • User579761722 posted

    I want to convert them from the web application

    Tuesday, June 26, 2007 5:19 AM
  • User-1091210821 posted

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

    save as jpeg...

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

    but again this uses system.drawing

    Tuesday, June 26, 2007 5:35 AM
  • User478895623 posted

    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

    Tuesday, June 26, 2007 7:41 AM
  • User-1091210821 posted

    BPW, if you have a questio

     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.

    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.

    Tuesday, June 26, 2007 7:50 AM
  • User-748968968 posted

     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.

    Friday, September 5, 2008 12:22 PM