问题
I have a .net 3.5 application that will be dealing with a large number of images. I need to check that the image extension is correct, the image height and width, and the PPI. I do not want to load the entire image into a .net image or bitmap, this will take to long and be to resource intensive. I can not use third party plug-ins or dlls, and of course it needs to be done yesterday.
So, I am reading the initial bytes of the files, checking the "magic" numbers to make sure the image extension matches, and then the height and width of the image for most of the image types I need to handle. This is much faster and less resource intensive. I could use a little help reading the PPI from some of the image types, and two of the types have just stumped me beyond validating the extension so far.
BMP, JPG, GIF, and PNG I need help reading the PPI.
- Looking for something like located at offset xx.
TIF, EPS, and PSD I need help reading height, width, and PPI.
I am pretty much stuck on Eps and Psd files, and anything would help.
Yes I know about tiflib, it looks great, and way more than I need. A lighter version that handles only the height, width, and PPI would be great. If I have to I can do this, but I'm hoping someone all ready has :-)
回答1:
All byte locations assume the first byte is in position 1, not 0.
PNG files Width: bytes 9-12, Height: bytes 13-16, PPI: look for a 4 byte signature of 112 72 89 115 (decimal values), bytes 1-4 (following) contain the X pixels per unit, bytes 5-8 contain the Y pixels per unit, byte 9 contains the unit specifier (0=unknown, 1=meter). The PPI is stored in an optional chunk and may not exist in all PNGs.
http://www.libpng.org/pub/png/spec/iso/index-object.htm or http://en.wikipedia.org/wiki/PNG_file_format
BMP files Width: bytes 18-21, Height: bytes 22-25, PPI: bytes 38-41 contain the X pixels per meter, bytes 42-45 contain the Y pixels per meter.
http://en.wikipedia.org/wiki/BMP_file_format
JPG files JPEG refers to the compression, while JFIF is the actual file storage format. Width: , Height: , PPI: bytes 11-12 contain the X pixels per unit, bytes 13-14 contain the Y pixels per unit. Byte 10 contains the unit (0=no units, 1=pixels per inch, 2= pixels per cm).
http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format and http://www.ecma-international.org/publications/files/ECMA-TR/TR-098.pdf
GIF files Width: bytes 7-8, Height: bytes 9-10, PPI: GIF files do not contain any pixel density information.
http://en.wikipedia.org/wiki/Graphics_Interchange_Format
I have supplied links to the other formats as they require specific knowledge of the format to determine if or where the information you requested is stored.
http://partners.adobe.com/public/developer/tiff/index.html
http://en.wikipedia.org/wiki/Portable_Document_Format and http://www.adobe.com/devnet/pdf/pdf_reference_archive.html
http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
回答2:
Rather than spending hundreds of hours of development time writing and debugging your own multi-format image parser, I would suggest that you look at ways to optimize existing methods. While some image formats are easy, others are hard. Some are really hard. As was mentioned, some "formats" are just containers for other formats.
Here are some suggestions:
Speed up loading an image from disk in a windows forms (c#.net) app
http://www.vcskicks.com/fast-image-processing.php
How can I find the pixel per inch value in a JPG image?
来源:https://stackoverflow.com/questions/14390204/reading-image-header-info-without-loading-the-entire-image