I am using Image.RotateFlip() to fix the orientation of some photos. I read the EXIF information of each photo and then I rotate/flip the image accordingly to make it straight up. My code is something like this:
//get info before processing.
var imgf = img.RawFormat;
//rotate and/or flip.
img.RotateFlip(rfty);
//reset orientation info.
img.RemovePropertyItem(km_iPropertyTagOrientation);
img.RemovePropertyItem(km_iPropertyTagThumnbailOrientation);
//save fixed image to a new file.
string strFilenameNew = Path.ChangeExtension(strFilename, "temp");
img.Save(strFilenameNew, imgf);
//replace original file.
File.Delete(strFilename);
File.Move(strFilenameNew, strFilename);
As you can see, I rotate/flip the image, remove the EXIF information that encoded the old orientation, and lastly I save it in the same format and replace the original file.
When I upload my photos on to http://metapicz.com, this is what I get:
Source photo (rotated 90 degrees, with orientation data): image looks rotated, landscape aspect ratio, orientation shows as Rotate 90 CW. This is what you would expect.
Fixed photo (processed by code above): image looks straight up, portrait aspect ratio, orientation data not reported. This is also what you would expect.
So far, so good. Now, I use Image.GetThumbnailImage() to obtain a thumbnail of the fixed photo, by scaling its size by 20%. The odd thing is, the resulting thumbnail is rotated again! When I upload it on to http://metapicz.com, this is what I get:
Thumbnail photo (obtained from fixed photo): image looks rotated, portrait aspect ratio, orientation data not reported. This is awkward, because the photo is now distorted, since the aspect ratio is preserved from the fixed photo (portrait), but the image in the photo looks rotated as in the source photo!
You can find this set of photos (source, fixed and thumbnail) here: https://verdewek-my.sharepoint.com/personal/cesargon_verdewek_com/_layouts/15/guestaccess.aspx?folderid=0d56479242b154fb7a31ae9a96f7be9cb&authkey=AbT_VQeoKU2EoVDuhvM2RgM
Any idea what I may be missing? Thank you.
Related
I have made a thumbnail of an image using Image.GetThumbnailImage Method.
Now I want to get the original image and display that original image to a different pictureBox when I click on the thumbnail (which is a different pictureBox).
Does anyone know how can we do that? I have search on the Internet and all I found is create a thumbnail, I didn't find any method to retrieve back the original image
If you want to have thumbnails which lead to an original version of the image, you need to at least keep and store the original version of the image.
When the user clicks the thumbnail, display the original version of the image that you stored.
You cannot restore original images from a thumbnail
Given a thumbnail of an image, you can't expand it back into the original image. Scaling it down into a thumbnail is a destructive process which removes the vital information.
Consider... given this thumbnail:
How could you recreate this image?
Most of the information in the larger image is missing in the thumbnail. The cracks in the paint aren't there in the thumbnail. Nor can you spot her veil, make out the background, see the white flecks on the canvas, or notice the waviness of her hair. You can't see the ripples of her dress nor the folds and creases in the fabric. In the thumbnail, all those things no longer exist: the scale down has destroyed all that information.
Trying to reconstruct the larger image from the smaller would be like akin to recreate a 20-page novel from a half-page summary of its major points: you can guess at how the author might have written it, but you'll never know word-for-word what it was originally.
The image of the Mona Lisa was from Wikipedia.
I would like to be able to add an image to a live tile so that there is no stretching and the image looks normal. I am getting my images from the PhotoChooserTask, which contain images from the medialibrary. As of now I can successfully place an image on a live tile, but it is stretched and the aspect ratio is not correct. How would I be able to find the aspect ratio of the image and crop the image so that it ends up being a square with dimensions 173x173 with no streching? I have followed a couple tutorials found online but nothing seems to accomplish this the way I need it to.
I accomplished this by referencing Resize image for Live Tile - WriteableBitmapEx which proved to have the correct implementation.
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...
With a mobile device I take a picture of a flat light object on a dark surface. (for instance a coupon clipped out of a newspaper).
The image is then run through a brightness/contrast filter. If it is too dark, vital components are left out. If it is too bright, the writing on the coupon is lost.
This image is then converted into a bitonal image. Any pixel that is 50% or more dark is converted to black, everything else is white. (done)
I am left with a skewed bitonal image (think of a white trapezoid inside a larger rectangle with a black background).
I need to figure out how to crop the image - which when it's on a black background is easier than when it's on a white background. Then, I have to de-skew the image so it is rectangular instead of trapezoidal, while attempting to preserve aspect.
The end result should be a nicely cropped, bitonal, readable image of the coupon.
To crop your image, you can use the LockBits method and scan through all your pixels to find the first pixel with content from the top, left, right and bottom, respectively. How to use LockBits is described nicely here: https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx
Assuming your image is not rotated, and that the skewing comes from the camera held at an angle against the table where the coupon is being photographed, you should now have a skewed image of the coupon, fitting perfectly within the bounds of the cropped bitmap. You should also know the four corners of the trapezoid.
"Undistorting" an image is not as easy as you might think though. However, good people have solved this problem and you can probably port their code to your own use. Here is a link I used to explore this problem in a similar case some time ago:
http://ryoushin.com/cmerighi/en-US/2007-10-29_61/Image_Distortion_Enhancements
I also have some code stored somewhere if you can't make any sense of what you find.
I'm working on an application that prints a folder of image files, including JPEG and TIFF. The TIFF images are usually Black and White (1bpp).
After loading the image, I want to determine if the image is Color or B&W or Grayscale so I can send the image to the right printer (color printer or black and white printer).
I'm using the Bitmap constructor image = new Bitmap(filename); to load the image.
EDIT: The answer to check the pixel depth is great for B&W. Any ideas on checking if the image is grayscale without iterating through every pixel?
Just check this property
image.PixelFormat
It will match one of the values in System.Drawing.Imaging.PixelFormat
Though you would want to send more than just Black & White to the B&W printer, you should also send any gray scales there as well.
The proper way to check this is:
For JPEG files you should check the appropriate properties using the PropertyItems collection of the Bitmap. This may contain the appropriate EXIF tags to help determine the bit depth. The next step would be to parse the JPEG header and look for the 'start of frame' marker and then the number of components in the image.
The final method is to load the JPEG into a Bitmap object and compare the number of pixels with the forumla (width * height * bytes_per_pixel). So if you load the bitmap and number of bytes of actual raw data is equal to (width * height) then you know it's a safe bet that the image has 1 byte per pixel and as such is grayscale.
The last thing you'll want to check is the PixelFormat of the bitmap itself.
For the TIFF file format you should do the same thing using the PropertyItems collection and check the appropriate tag mentioned in the spec. If these fail then do the image byte comparison and finally use the PixelFormat property as a last resort.