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

百般思念 提交于 2019-12-21 05:56:00

问题


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


回答1:


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");



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/857147/how-can-i-crop-image-without-changing-its-resolution-in-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!