简体   繁体   中英

Determining resolution (DPI) of an uploaded image

I have a component for an image file upload.

However, I want to also be able to check DPI settings because these images will eventually be printed and submitted on paper.

Within ASP.net, I can usually do something like this:

using (var rawBitmap = new Bitmap(postedFile.InputStream)){
    var dpi = (decimal)rawBitmap.VerticalResolution/bitmap.Height;    
    // do other stuff.
}

However, within silverlight, I don't have access to the same libraries in order to do this (that said, this is my first stab at Silverlight, so if there is a way to get those dlls in, I'm all for it, but I couldn't get my utility wrapper imported).

I've seen lots of recommendations for FJcore (imagetools also wraps this library), a JPEG encoding/decoding utility. In theory, one loads up the JPEG stream into the decoder and gets information out.

I've tried using the approach with FJcore, but all the files that I'm saving out of photoshop seem to be missing the correct header that indicates the star of the file, which causes the decoder to fail. I've also confirmed this issue using their unit tests.

Any ideas on how to pull image resolution out of a file upload in silverlight?

DPI of the image is not always stored on the image. This is usually an extra property saved as a metadata during capture by the scanner (or the camera). You can actually see that if you load a JPEG in C# using Bitmap and save it again, DPI property is lost and set to the default 96.

So this is unfortunately not an option always reliable. I do not think there is any chance of getting it for all images. DPI in fact is irrelevant for pictures that are not created by scanners.

Try FJ core assemblies to find DPI of image in silverlight

FileStream stream = imageFiles.OpenRead();
DecodedJpeg jpegImage = new JpegDecoder(stream).Decode();
int imageDpi = jpegImage.Image.DensityX;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM