【发布时间】:2020-06-01 16:03:22
【问题描述】:
我有这段代码可以调整图片大小:
public static void GetProductSmallThumbnail(string fileUrl, string fileName)
{
System.Drawing.Image.GetThumbnailImageAbort myCallback =
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image img = null;
FileStream fs = null;
try
{
fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
img = System.Drawing.Image.FromStream(fs);
int orgwidth = img.Width;
int orgheight = img.Height;
int imgwidth = img.Width;
int imgheight = img.Height;
// if 240 is max width
imgwidth = 240;
imgheight = Convert.ToInt32((imgwidth * orgheight) / orgwidth);
System.Drawing.Image myThumbnail = img.GetThumbnailImage(
imgwidth, imgheight, myCallback, IntPtr.Zero);
myThumbnail.Save(Path.Combine(Directory.GetCurrentDirectory(), "assets/products/small/" + fileName + ".jpg"));
}
finally
{
fs.Close();
}
}
生成的图像具有正确的宽度和高度,但大小为 89 KB,但我希望此图像为 7 KB
【问题讨论】:
-
@michasaucer 无论如何都不会将其保存为 JPG 吗?
-
OP,你确认
myThumbnail.Save(确实生成了一个jpeg文件吗? -
亲爱的@michasaucer 我该怎么做?
-
在我自己的测试中,您的代码生成了一个带有 .JPG 文件扩展名的 PNG 文件。
-
哦!如何纠正?
标签: c#