C# 如果下载了图像,则上载和调整大小的图像不会在图像编辑器工具中打开
我使用下面的代码来调整图像大小后,我上传它。函数重新调整图像的大小 在不更改文件扩展名的情况下使用新名称保存,图像在所有主要浏览器上都能完美显示 问题是只有当有人下载图像并尝试使用任何图像编辑器(如Fireworks或Photoshop)打开它时,它才会出现以下错误 错误:*无法打开文件。未知的文件类型。* 我不知道它为什么会出现这样的错误 调整图像大小的功能。C# 如果下载了图像,则上载和调整大小的图像不会在图像编辑器工具中打开,c#,asp.net,webforms,image-resizing,C#,Asp.net,Webforms,Image Resizing,我使用下面的代码来调整图像大小后,我上传它。函数重新调整图像的大小 在不更改文件扩展名的情况下使用新名称保存,图像在所有主要浏览器上都能完美显示 问题是只有当有人下载图像并尝试使用任何图像编辑器(如Fireworks或Photoshop)打开它时,它才会出现以下错误 错误:*无法打开文件。未知的文件类型。* 我不知道它为什么会出现这样的错误 调整图像大小的功能。 public static void ResizeImageInput(string OriginalFile, string
public static void ResizeImageInput(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (OnlyResizeIfWider)
{
if (FullsizeImage.Width <= NewWidth)
{
NewWidth = FullsizeImage.Width;
}
}
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, MaxHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
NewImage.Save(NewFile);
}
public static void ResizeImageInput(string OriginalFile、string NewFile、int NewWidth、int MaxHeight、bool only resizeifwidth)
{
System.Drawing.Image FullsizeImage=System.Drawing.Image.FromFile(原始文件);
//防止使用图像内部缩略图
FULLSIZEMAGE.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone);
FULLSIZEMAGE.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone);
如果(仅限ResizeIfWide)
{
如果(FullsizeImage.Width尝试添加要保存的图像格式,则这适用于gif图像
NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Gif);
即使指定了扩展类型,DoesNot work仍会出现相同的错误。GDI提供的GIF编码非常差。最好使用八叉树量化算法或PNG/JPEG。请不要在asp.net web表单中添加“.asp.net C”您的标题也是如此。它只是复制了标记。请注意,在ASP.NET应用程序中使用System.Drawing命名空间是非常糟糕的,除非您这样做。您当前在每次调用时都会泄漏NewImage
,如果发生异常,则会泄漏FullsizeImage。您可能会这样做。
NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Gif);