Extract size of the image embedded in a PDF - c#

I have an image embedded in a PDF file. I was able to extract the resolution of the image. However, if i crop the PDF using iTextsharp and a part of the image is cropped in the process. The new image continues to have the same resolution. By resolution I mean the dimension in the form of Width x Height. The cropped image is supposed to have a smaller size. How can I extract the size of the image in inches, if possible, so that I can differentiate the original image from the cropped pdf?

Embedded images in PDFs are never "cropped" in the sense that the "cropped-away" parts are gone forever. They are only cropped in the sense that these parts are hidden or masked.
If the image data inside the PDF say /Height 216 and /Width 288 then this is the size in Pixels (not inches or any other length unit). And "resolution" is then secondary:
if the PDF environment commands that this image should be rendered onto a square of 3x4 inches, its resolution in this moment is 72x72 dpi.
if the PDF environment commands that this image should be rendered onto a square of 1.5x2 inches, its resolution in this moment is 144x144 dpi.
However it can well be that the image is only partially visible ("cropped"), maybe because half of it is rendering beyond the page borders...

Related

How to find the dimensions of an SVG image so that there is no white space around the graphics and the text remains readable?

I process C++ generated SVG images which are generated to given pixel width and height.
How do I C#-determine the optimal width and height dimensions for displaying the SVG images so that there is not much white space / margin around the image and the text in the images remains readable?
That is if I specify the font size for the image and redraw the image, how do I determine the target optimal dimensions in pixel guaranteeing the text is in the given font size and there is not too much white space around the image?

Why does GetThumbnailImage create blurry images

I am creating thumbnails of images. I've implemented the thumbnail display in two ways
1 - given an image, create a new thumbnail using the above method.
2 - given an image, grab the dimensions, calculate the resized dimensions and then set that as the dimensions of the PictureBox. The PictureBox is set to auto-zoom mode, so it will scale the image down to 1/4.
Both methods generate thumbs that are 1/4 the original size of the input image (display only, I am not worried about saving or working with the image), but the second method produces a much clearer thumbnail, although I guess it would use more memory since I don't think zooming the control actually affects the image.
Why is the resulting thumbnail very blurry when GetThumbnailImage is used?
See the remarks in the documentation:
If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image.
The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.
So it can happen that you get an up-scaled embedded thumbnail.

The difference in image resolution (ppi) between C# and Photoshop

For example, C # says that the selected image contains 96 ppi, while that same image in Photoshop contains 72 ppi.
Why is there a difference?
I’m inclined to trust Photoshop in this case, and how to test image resolution if C# returns false results?
We need to build some sort of validator control that rejects all images with ppi != 300.
Control should support the following formats: jpg, jpeg, gif, png, bmp.
Code is listed below:
Image i = Image.FromFile(FileName);
Console.Write(i.VerticalResolution);
Console.Write(i.HorizontalResolution);
DPI means dots (pixels) per inch. The physical size in inches is subjective, based on the current monitor's size and resolution. Unless you're relying on metadata (which gif and bmp don't contain) you cannot reliably calculate this.
Photoshop simply has a prescribed value for DPI, which it uses when translating images for print. This value is stored in the PSD file and may be copied to JPEG metadata, but if you save the image in a format without DPI metadata, the information is not stored.
Update:
The reason your code gets a different value is that C# fetches its VerticalResolution and HorizontalResolution values from the current DPI setting on the computer. Photoshop's DPI is for use with print, so it knows the physical dimensions if you want to send your image to a printer. It has a default value of 72dpi, but you can change this. The value has no meaning on a screen, though, since screens deal in pixels only.
DPI means dots per inch. A bitmap image does not have an inherent DPI, it merely has a size which is the number of pixels in the horizontal and the number of pixels in the vertical (width and height). An image only gains a resolution (in DPI) when you say how many pixels you want to squeeze into each inch.
So if I have an image that is 100 pixels wide and 100 pixels high (100px × 100px), it will be 100 DPI if I print it (or convert it into a format that dictates the print size) such that it fits exactly in a one square inch (1" × 1"). It will be 50 DPI if I print it to fit in a square that is two inches by two inches, &c.

Image size considering (or not) h/v resolution

I have two images:
Name Type Width Height HRes VRes
img01 GIF 256 256 384 96
img02 TIFF 1728 1147 200 100
Windows internal viewer shows them using different patterns:
img01 is displayed as 256x256 image (square)
img02 is displayed as a vertical image (despite of width>height) and image format is correct
After seen img02 behaviour I thought that displayed size is given by:
Display_Width = max(HRes,VRes) * Width / HRes
Display_Height = max(HRes,VRes) * Height / VRes
But that's not good for img01, which is displayed as 256x256!!
If I use Image class to load those images, resolution is not taken in account, so img02 is shown horizontally and distorced respect correct format.
If Windows internal viewer shows them correctly, I think there should be some property to understand if resolution should be considered or not.
With Image class I have Flags and PropertyItems properties, but I really don't understand if they are the ones I have to look at.
Can you teach me the right method to display images (using Image class) without distortion?
Thanks
Aspect ratio in GIF files is very uncommon, since GIF is a format used to store graphics intended to be displayed on computer screens. In fact, GIF87a files do not have an aspect ratio at all, and GIF89a do not store the actual aspect ratio but an approximation.
On the other side, TIFF files for the most part are used by graphic designers, photographers and publishers, so the concept of pixel aspect ratio is core to the format as it allows these files to move between different display mediums.
My guess is that most GIF decoders ignore the aspect ratio from the GIF89a format. I think you should do the same.

How can I crop image without changing its resolution in C#.Net?

I made small program to divide large pictures and take part of them.
When i import image that made by "Microsoft Paint" this image is "96 dpi" so my program doing well.
But I've pictures that made by Photoshop its resolution is 71.6 dpi when i crop these pictures the new cropped picture take 96 dpi resolution so the size is deference between them.
I want to crop the picture with keeping its resolution as it.
.
thank you very much
Bitmap.clone lets you create a cropped copy of an image, which you can then save. It shouldn't change resolution or anything (the image will look bigger if you open it in a program that zooms in more when images are smaller). It cannot be used to expand the canvas (you'll get out of memory errors). So, just grab an Image from file, cast to Bitmap, (system.drawing namespace) and clone it to be smaller, then save it.
Example:
using System.Drawing;
//...
Bitmap x = (Bitmap) Image.FromFile(#"c:\tmp\food.png");
Image x2 = x.Clone(new Rectangle(25, 25, 50, 50), x.PixelFormat);
x2.Save(#"c:\tmp\food2.png");
DPI (dots per inch) is just a relationship between pixel size and the size on a medium. If you have an image which is 1024 x 768 pixels, it is 1024 x 768. There is no inherent DPI attached to a bitmap/binary file.
If you want to print that image on a printer which prints at 300 dpi, then you can calculate the size on the paper, for example.
The SetResolution() method of the Bitmap class allows you to specify the resolution of an image.
See http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.setresolution.aspx

Categories

Resources