HighTechTalks DotNet Forums  

Huge images

Dotnet Framework (Drawing) microsoft.public.dotnet.framework.drawing


Discuss Huge images in the Dotnet Framework (Drawing) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Antimon
 
Posts: n/a

Default Huge images - 02-04-2007 , 10:03 PM






Hi,
I've been working on a website and i need to resize uploaded pictures
and stuff. Everything works fine with Image.fromstream method but
there seems to be a simple problem,
I created a simple solid blue jpg file which is 10000x10000 pixel
size. Only 500 bytes as its jpg. But if i upload that file, it passes
the content length limits and when my code tries to read it
(Image.FromStream) it uses 100% cpu for a couple of seconds, grows
memory usage and throws an outofmemory exception. It would kill the
system performance but i could not find any way to read image size
without reading image. So i can force some limits before reading
image.
Any suggestions?


Reply With Quote
  #2  
Old   
Bob Powell [MVP]
 
Posts: n/a

Default Re: Huge images - 02-05-2007 , 06:44 AM






AFAIK the only way to do this is to open the image as a binary stream,
decode the headers, read the sizes and close the stream before reading all
of it. This implies that you need to know the file header structures for all
the popular image file formats.

Opening a file with Bitmap.FromStream will expand the image to it's full
size and take up the corresponding amount of memory.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Antimon" <antimon (AT) gmail (DOT) com> wrote

Quote:
Hi,
I've been working on a website and i need to resize uploaded pictures
and stuff. Everything works fine with Image.fromstream method but
there seems to be a simple problem,
I created a simple solid blue jpg file which is 10000x10000 pixel
size. Only 500 bytes as its jpg. But if i upload that file, it passes
the content length limits and when my code tries to read it
(Image.FromStream) it uses 100% cpu for a couple of seconds, grows
memory usage and throws an outofmemory exception. It would kill the
system performance but i could not find any way to read image size
without reading image. So i can force some limits before reading
image.
Any suggestions?




Reply With Quote
  #3  
Old   
Markus
 
Posts: n/a

Default Re: Huge images - 02-05-2007 , 06:53 AM



Quote:
AFAIK the only way to do this is to open the image as a binary
stream, decode the headers, read the sizes and close the stream
before reading all of it. This implies that you need to know the file
header structures for all the popular image file formats.
I never tried, but: If you right-click an image in Windows Explorer, you
get the file properties - for images it also includes the width/height
of the image.
Reading these properties might be an option (however - I can't give you
further information, as I never needed that and it might not be correct
for all image files).

hope this ideas helps for your solution
Markus


Reply With Quote
  #4  
Old   
Antimon
 
Posts: n/a

Default Re: Huge images - 02-05-2007 , 08:43 AM



Quote:
AFAIK the only way to do this is to open the image as a binary
stream, decode the headers, read the sizes and close the stream
before reading all of it. This implies that you need to know the file
header structures for all the popular image file formats.
As i just need "BMP, JPG, GIF, PNG" support, i did some research and
managed to read GIF, PNG and BMP dimensions from headers, JPG seems to
be a little tricky but i'm working on it. This way i can extract
information by just reading not more than 100 bytes.

But some "fetch info w/o reading the whole image to memory" class
would be fine in Drawing namespace.

Quote:
Reading these properties might be an option.
Yep, explorer seems to be doing this exactly

Thank you both for helping.



Reply With Quote
  #5  
Old   
n!
 
Posts: n/a

Default Re: Huge images - 02-05-2007 , 12:23 PM



An alternative, which may not be what you really want but I'll note anyway.

You could link to the managed Direct3D assemblies and use the:

Microsoft.DirectX.Direct3D.TextureLoader.ImageInfo rmationFromStream

method, which obtains information about an image from the specified data
stream. It doesn't require a DirectX device to be created or anything, so
can be called directly. It provides with the images bitdepth, format, width
and height should you require them and supports .bmp, .dds, .dib, .hdr,
..jpg, .pfm, .png, .ppm and .tga file formats. Doesn't support .gif however,
but at least that's only one format to write custom support for instead of
four

Anyway, as I said, not sure if that's of interest to you but just noting it
in case.
n!



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.