Change resolution with Bit Miracle's LibTiff.Net - c#

I use Bit Miracle's LibTiff.Net to convert scanned image to tiff file, but I want to save images with 200dpi even that scanned with 300 dpi I found this in the Bit Miracle site sample
tif.SetField(TiffTag.XRESOLUTION, img.HorizontalResolution);
tif.SetField(TiffTag.YRESOLUTION, img.VerticalResolution);
but I make this change to set resolution always 200dpi :
tif.SetField(TiffTag.XRESOLUTION, 200);
tif.SetField(TiffTag.YRESOLUTION, 200);
in the tif file property it say 200 dpi but the file size don't change 163 Ko before and after the change,
I make a search for all resolution used in the code, and I make it 200 but always the same file size.
if I change the value in the SetField I got it in the file property but not in the rezolution of the image (same size always for all resolution test)
How I can change the resolution of the destination tif ?

LibTiff.Net is a codec library. It's not designed to resample images.
Changing dpi value will only affect print size of the image, not the image data. You should resample data yourselves.

Related

reduce the size of image to fixed size in c#

I want to reduce the the size of an image to fixed size using C# windowsforms.
For suppose am taking an image with size 3 MB , i want the output of the image size is in between 100 to 200 KB.
Note: The size of an input image may be vary but i need the output will be the same
There is no built-in functionality to resize an image to a given file size that I am aware of. The size can be reduced by various methods:
Changing to a compressed file format.
Shrinking the dimensions of the image.
Lowering the quality of the image.
The system would need a loop that attempts to resize the image using one more of the above methods, check what the resulting file size is, and then adjust the parameters and try again until the desired size is reached.
A more common way to deal with this is to shrink the dimensions of the image to a height and width that generally yields acceptable sizes and not worry about the exact size of any one file. There are numerous 3rd party libraries that will easily do this (I've used ImageResizer successfully), or try this.

bitmap.save is not saving exact pixel color in c#

I've changed all pixels color in one bitmap file with "bitmap.SetPixel" .when I save the bitmap on PC , the saved file has different colors for each pixel when I open it in photoshop .. !! the saved colors are close but not the same.. please help me.
set pixel color:
Bitmap.SetPixel(x, y, MyColor);
save bitmap :
Bitmap.Save(MyPath, PicImage.RawFormat );
Note that Image.RawFormat is basically a shorthand for 'use the original image format!'
So, if your image source is a lossy compressed format like jpeg you are saving , i.e. re-encoding it in the same compressed format, but, most likely using a different compressor codec and/or different quality etc parameters.
So you can expect some slight color shifts.
Even with the very same codec etc a jpeg would have to create a different version of the compression tables just for the one pixel you are changing..
Make sure the original image uses a lossless compression, like png or some tif formats to avoid this effect.
To be sure you may want to specify the format explicitly.

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