This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get the resolution of a jpeg image using C# and the .NET Environment?
In a batch image downloader I am coding, I use a WebClient (DownloadFile) to save images from given urls. Is there an easy way to get the resolution of these images? If the WebClient cannot, how can i get the resolution after the file is saved?
To get the DPI use the following:
Image image = Image.FromFile("image.jpg");
image.HorizontalResolution;
For other things such as height, width, and size, view this stackoverflow question which has many good answers, (including mine :D ).
If you save the image after downloading it from the WebClient, you can use the following:
Image img = Image.FromFile(#"image.png");
Console.WriteLine(img.Width + "x" + img.Height);
This will give you the width x height of the image, for example, 1920x1080.
Related
This question already has an answer here:
Take screenshot from window content (without border)
(1 answer)
Closed 4 years ago.
I am trying to use Form.DrawToBitmap to get an image of the current form. It works fine but shows the window border. How could I get it to create a bitmap of the form without the border.
Note: I'm aware that changing the form border style to "None" would work but switching the form border style twice (to "None" and back) at runtime is not really something I want every end-user to be seeing every time this function procs.
First use DrawToBitmap method and then crop the image by cloning it and give your desire rectangle somthing like:
private Bitmap ConvertToImage()
{
using (var bitmap = new Bitmap(this.Width, this.Height))
{
this.DrawToBitmap(bitmap, this.Bounds);
return bitmap.Clone(new Rectangle(DESIRE_X, DESIRE_Y, DESIRE_WIDTH, DESIRE_HEIGHT), bitmap.PixelFormat);
}
}
This question already has answers here:
Getting image dimensions without reading the entire file
(9 answers)
Closed 7 years ago.
Width and Height of an image inside a jpg file can be retrieved by reading all file data into an Image.
System.Drawing.Image img = System.Drawing.Image.FromFile("a.jpg");
var w = img.Width
var h = img.Height
This is costly however and if only width and height are needed, there might be a better way.
So I wonder if there is a less resource intensive way to get the image dimensions. In my (common) scenario, the jpg is uploaded to the web server and the web application shall just get width and height and store that. I thought, the jpg file format might have width and height as easy readable metadata and if so, someone knows how to access it.
You may want to parse the file header directly instead of constructing an image, the width and height are in there
http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#File_format_structure
This question already has answers here:
Using Graphics.DrawImage() to Draw Image with Transparency/Alpha Channel
(2 answers)
Closed 8 years ago.
I'm trying to find a solution how to make an image's background transparent. I'm programming a small and easy 2D game with a scrolling background that displays a sky.
You'll see the code if you follow the link: Avoiding creating PictureBoxes again and again
picBoxImage = new PictureBox();
picBoxImage.Image = new Bitmap("Path");
picBoxImage.SetBounds(100, 100, 100, 100);
this.Controls.Add(picBoxImage);
The code above shows the inclusion of the image to the Windows Form.
Now the object (let's say the image does show an aircraft) has been set over the other images showing the sky.
How is it possible to set the background of the aircraft's image transparent?
It is important that the aircraft is seen without any perturbing white background.
Would you mind giving me some code to solve the problem?
I don't know if you're talking about what kind of image file or maybe the css that gives you that "effect".
I think you're talking about the image file. In that case, .png files let you to set transparent background.
If it wasn't what you are looking for,...sorry.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm writing a small Login dialog, and have embedded a banner at the top of the dialog for aesthetic reasons. All went well, except that by default, WPF anti aliases the entire image, making the text that was contained within it frustrating blurry.
After a bit of searching, the first few pages of results showed that it's common belief that anti aliasing cannot be disable in WPF. Can any confirm, or otherwise deny this?
It's a minor issue for me - I'll take the text out of the image and instead superimpose a label with the same text on top of the background image to achieve the same effect (though I must admit, it's a bit annoying).
Thanks,
Rob
As far as I know, WPF always does anti-aliasing when scaling a bitmap. However you should be able to accomplish your goal by avoiding the bitmap scaling.
There are two steps:
Set SnapsToDevicePixels="true" on your image
Set a ScaleTransform on your image to scale it so that one device pixel = one bitmap pixel
To compute the needed ScaleTransform, compute your screen's DPI like this:
var DPI = Win32Functions.GetSystemMetrics(SM_CYICON) / SystemParameters.IconHeight * 96;
and then for the bitmap, do:
var scale = bitmapDPI / DPI;
var transform = new ScaleTransform(scale, scale);
This will cause your bitmap's pixels to exactly match with the device pixels. WPF will not stretch the bitmap, so there should be no anti-aliasing.
If you do want to stretch your image on high DPI screens but do so without anti-aliasing (eg double all pixels), just stretch the bitmap in your own code using whichever algorithm you like and use the above with the stretched bitmap.
It's not really anti-aliasing - it's sub pixel positioning that causing the problem, I've written about it (and about a control that solves the problem) on my blog at:
http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx
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.