Why does GetThumbnailImage create blurry images - c#

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.

Related

C# Fast load Bitmap into PictureBox

I receve Bitmap image from a camera at 30 fps, and I need to display all images in a pictureBox.
The problem is that the PictureBox is very slow!
I have try to implement a custom PictureBox with DoubleBuffer enabled but the problem is not resolved.
Do you have a custom PictureBox or an user control or a solution that can display the image faster?
Additional information:
The image resolution is 2048x1088 with 256 graylevel (8bit image).
I use AForge.NET for elaborate the images.
Thank you
That image gets expensive to draw when it has to be resized to fit the PB's client area. Which is very likely in your case because your images are pretty large. It uses a high-quality bi-cubic filter to make the resized image look good. That's pretty expensive, albeit that the result is good.
To avoid that expense, resize the image yourself before assigning it to the Image property. Make it just as large as the PB's ClientSize.
That's going to make a big difference in itself. The next thing you can do is to create the scaled bitmap with the 32bppPArgb pixel format. It's the format that's about 10 times faster then any other because it matches the video adapter on most machines so no pixel format conversions are necessary.

Retrieve original image from thumbnail

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.

Extract size of the image embedded in a PDF

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...

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

How to serve high-resolution imagery in a low-resolution form using C#

Trying to use 300dpi tif images for display on the web. At the moment, when the user uploads an image, I am dynamically creating a thumbnail. If a page is created referencing the high-res image with a width of 500x500px, can I use the same functionality to convert to a gif/jpg on the fly. What is the impending resolution of the jpg that would be created?
EDIT:
To further explain the usage, the user uploads 300dpi images which are approx 3000x3000 pixels. The user is using these images to create a catalog page which will be used for pdf printing. When they are creating the page, we only need 72dpi images to display to the screen, but for print, need the 300dpi images. Obviously they do not want to add a 3000x3000px image to the page, so it needs to be resized to the correct viewing area, e.g. 500x500px etc.
This boils down to a simple image resize. The discussion of DPIs is just ancillary data to calculate the scale factor.
As #Guffa said, you should do this at the time of the upload so that you can just serve static images in your viewer.
This will be a load on the server:
Load the full image. This will be about 27 MB of memory for your 3000x3000 images.
Resize. Lot's of math done lazily (still CPU intensive).
Compress. More CPU + Cost of writing to your drive.
Since you are already taking the time to generate a thumbnail, you can amortize that cost and this cost by not having to repeat Step 1 above (see the code).
After an image is uplaoded, I would recommend spinning off a thread to do this work. It's a load on the web server for sure, but you're only other option is to devote a second machine to performing the work. It will have to be done eventually.
Here is some code to do the job. The important lines are these:
OutputAsJpeg(Resize(big, 300.0, 72.0), new FileStream("ScreenView.jpg"));
OutputAsJpeg(Resize(big, bigSize, 64.0), new FileStream("Thumbnail.jpg"));
We can resize the big image however we need. In the first line we just scale it down by a fixed scale (72.0 / 300.0). On the second line, we force the image to have a final max dimension of 64 (scale factor = 64.0 / 3000.0).
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
BitmapSource Resize(BitmapSource original,
double originalScale,
double newScale) {
double s = newScale / originalScale;
return new TransformedBitmap(original, new ScaleTransform(s, s));
}
void OutputAsJpeg(BitmapSource src, Stream out) {
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(src));
encoder.Save(out);
}
// Load up your bitmap from the file system or whatever,
// then dump it out to a smaller version and a thumbnail.
// Assumes thumbnails have a max dimension of 64
BitmapSource big = new BitmapImage(new Uri("BigPage0.png",
UriKind. RelativeOrAbsolute));
double bigSize = Math.Max(big.PixelWidth, big.PixelHeight);
OutputAsJpeg(Resize(big, 300.0, 72.0), new FileStream("ScreenView.jpg"));
OutputAsJpeg(Resize(big, bigSize, 64.0), new FileStream("Thumbnail.jpg"));
If I understand what you want - you're trying to make a gif or jpg thumbnail of a very high resolution tif, for web display - if not, I apologize in advance....
If you want the thumbnail to be 500x500px, that is the resolution of the jpg/gif you'll want to create - 500x500, or at least 500x<500 or <500x500 (to fit in the box, unless you want distorted images).
For display on the web, the DPI does not matter. Just use the pixel resolution you wish directly.
Technically the JPG/GIF is created at 72-DPI by the Image classes in .NET. But the DPI really doesn't have any meaning to the browser - it just uses the dimensions 500x500.
When displaying an image on a web, the dpi (or more correctly ppi) setting is irrelevant. It's only the size in pixels that is relevant.
You can convert an image on the fly, but it is very work intensive for the server to do that every time the image is displayed. You should create the sizes that you need when the user uploads the image.

Categories

Resources