我想在c#中动态地裁剪图像。使用c裁剪图像或缩小比例#
我已经提到了一些链接和实现如下。
参考:
- http://www.c-sharpcorner.com/blogs/resizing-image-in-c-sharp-without-losing-quality1
- http://www.c-sharpcorner.com/blogs/resizing-image-in-c-sharp-without-losing-quality1
但我越来越低质量的图像。我在他们的服务器看到的网站,他们所上传图片和缩小网页不失品质
他们是如何做呢?为什么我们不能在c#中完成?
CODE
public static Image ScaleImage(Image image, int width, int height)
{
if (image.Height < height && image.Width < width) return image;
using (image)
{
double xRatio = (double)image.Width/width;
double yRatio = (double)image.Height/height;
double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(image.Width/xRatio);
int nny = (int)Math.Floor(image.Height/yRatio);
Bitmap resizedImage = new Bitmap(nnx, nny, PixelFormat.Format64bppArgb);
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.Clear(Color.Transparent);
// This is said to give best quality when resizing images
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(image,
new Rectangle(0, 0, nnx, nny),
new Rectangle(0, 0, image.Width, image.Height),
GraphicsUnit.Pixel);
}
return resizedImage;
}
}
我以前有过这个问题并解决了它,但我不记得自动柜员机和我在工作的方式。我不知道怎么说,你可以将它转换为JPEG,缩小它,然后回到位图,因为我认为位图的缩放比较不好......我不确定这甚至是我做的,但它感觉像是什么我做到了。已经有几年了......除非你测试并看到它,否则这将有所帮助。 –
[调整图像C#]的可能的重复(http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp) – Nino
裁剪不应该失去质量。如果你缩小它,你将永远失去信息。请务必为这两个图像设置__same dpi__设置! - 请参阅[这里是一个类似的问题](http://stackoverflow.com/questions/26612057/where-does-this-quality-loss-on-images-come-from/26614735?s=6|0.0626#26614735) 。注意第二部分! - 也[见这里关于你的像素格式](http://stackoverflow.com/questions/34590509/pixelformat-format32bppargb-vs-pixelformat-format64bppargb) – TaW