C# 创建缩略图(低质量和大尺寸问题)
public void CreateThumbnail(Image img1, Photo photo, string targetDirectoryThumbs)
{
int newWidth = 700;
int newHeight = 700;
double ratio = 0;
if (img1.Width > img1.Height)
{
ratio = img1.Width / (double)img1.Height;
newHeight = (int)(newHeight / ratio);
}
else
{
ratio = img1.Height / (double)img1.Width;
newWidth = (int)(newWidth / ratio);
}
Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg");
img1.Dispose();
bmp1.Dispose();
}
我设置了 700px
以便您可以更好地了解问题。 这是原始图像和调整大小 1。
有什么好的推荐吗?
谢谢,
岛
public void CreateThumbnail(Image img1, Photo photo, string targetDirectoryThumbs)
{
int newWidth = 700;
int newHeight = 700;
double ratio = 0;
if (img1.Width > img1.Height)
{
ratio = img1.Width / (double)img1.Height;
newHeight = (int)(newHeight / ratio);
}
else
{
ratio = img1.Height / (double)img1.Width;
newWidth = (int)(newWidth / ratio);
}
Image bmp1 = img1.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
bmp1.Save(targetDirectoryThumbs + photo.PhotoID + ".jpg");
img1.Dispose();
bmp1.Dispose();
}
I've put 700px
so that you can have better insight of the problem.
Here is original image and resized one.
Any good recommendation?
Thanks,
Ile
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该会发现我对这个问题的回答很有帮助。它包括一个用 C# 进行高质量图像缩放的示例。
我的其他答案中的完整示例包括如何将图像保存为 jpeg。
这是相关的代码...
You should find my answer to this question helpful. It includes a sample for high quality image scaling in C#.
The full sample in my other answer includes how to save the image as a jpeg.
Here's the relevant bit of code...
如果图像包含缩略图,它会自动将其拉伸到所需的大小...这会让它看起来像垃圾(就像你的情况;))
直接从 MSDN 中...
在你的情况下,我只是仔细检查了源图像的缩略图并得到了这个......
If the image contains a thumbnail it'll automatically stretch it to the desired size...which will make it look like crap (like your case ;))
Straight outta MSDN...
In your case I just double checked the source image for it's thumbnail and got this...
尝试将原始图像绘制到另一个较小的图像上,并保存结果。
Try to draw the original image to another smaller image, and save the result.
您可以使用第三方应用程序吗?如果是这样,您可能需要查看 ImageMagick 来管理缩略图创建。有一个 .NET 包装器。
http://imagemagick.codeplex.com/
Are you allowed to use third party applications? If so, you may want to check out ImageMagick to manage thumbnail creation. There's a .NET wrapper.
http://imagemagick.codeplex.com/
我编写了一个免费的 .dll 可以轻松完成此操作。如果您想查看文档,就在这里......
Git 存储库和教程
I wrote a free .dll that does this easily. It's here if you want to see the documentation....
Git Repository & Tutorial