如果下载了图像,上传并调整大小的图像将不会在图像编辑器工具中打开

发布于 2025-01-04 17:13:55 字数 4195 浏览 5 评论 0原文

上传图像后,我使用以下代码调整图像大小。函数调整图像大小 并用新名称保存它们,而不更改文件扩展名,并且图像在所有主要浏览器上都能完美显示。

问题是,只有当有人下载​​图像并尝试使用 Fireworks 或 Photoshop 等任何图像编辑器打开它时,才会出现以下错误

错误:*无法打开文件。未知的文件类型。*

我不确定为什么会出现该错误。

调整图像大小的功能。

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

上传图像的功能

protected void btnArticlePageImage_Click(object sender, EventArgs e)
{

    try
    {
        String filePath = string.Empty;
        String CurrentGUID = Guid.NewGuid().ToString();

        if (FileUpload2.HasFile)
        {
            string filename = System.IO.Path.GetFileName(FileUpload2.FileName);
            System.IO.FileInfo f = new System.IO.FileInfo(FileUpload2.PostedFile.FileName);
            double fileSize = (double)FileUpload2.FileBytes.Length;
            if (fileSize < 1024000) // 1 MB current size size in bytes 102400=100kb  512000 = 500kb
            {
                if ((f.Extension.ToLower() == ".jpg") || (f.Extension.ToLower() == ".png") || (f.Extension.ToLower() == ".gif") || (f.Extension.ToLower() == ".jpeg"))
                {
                    filename = CurrentGUID + f.Extension;
                    //string productGUID 
                    filePath = Server.MapPath("../ImagesArticles/") + filename;
                    if (System.IO.File.Exists(filePath))
                    {
                        return;
                    }
                    else
                    {
                        //Upload files
                        FileUpload2.PostedFile.SaveAs(Server.MapPath("../ImagesArticles/") + filename);
                        //objPages.PageBannerImageEn = filename;
                        Session["ArticleLargeImage"] = filename.ToString();
                        string errMsg = "File Uploaded Successfully";
                        lblImageUploadMessage1.Text = errMsg;
                        // ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
                        Helper.ResizeImage(filePath, filePath, 150, 80, true);
                    }
                    return;
                }
                else
                {
                    string errMsg = "File must be an Image type of .jpg, .png, .gif, .jpeg";
                    //client-side error
                    lblImageUploadMessage1.Text = errMsg;
                    return;
                }
            }
            else
            {
                string errMsg = "File size is greater the 1MB";
                //client-side error
                lblImageUploadMessage1.Text = errMsg;
                return;
            }
        }
        else
        {
            //lblMesg.Text = "Only type .jpg, .png, .gif are allow";
            string errMsg = "Cant Upload File due to some error";
            //client-side error
            lblImageUploadMessage1.Text = errMsg;
            return;
        }
    }
    catch (Exception ex)
    {
        Response.Write("ERROR MESSAGE : " + ex.Message.ToString());
    }

}

顺序:用户可以上传图像类型 JPG、GIF、PNG 等。我仅使用 GUID 重命名图像并保持图像扩展名相同然后将其保存到网络服务器上。之后我重新调整图像大小以供以后使用。

这些调整大小的图像在所有浏览器中都可以正常工作,但唯一的问题是,如果有人下载这些图像进行编辑,它将无法在任何图形编辑器工具中打开。

它给出了问题第一部分中提到的常见错误消息。

I use following code to resize the image after i upload it. function re-sizes the images
and save them with new name without changing the file extension and image are displayed perfectly on all major browsers.

Problem is only when some one download the image and try's opens it with any image editor like Fireworks or Photoshop it will then give following error

ERROR: *Could not open the file. Unknown file type.*

I am not sure why it give that error.

Function to resize image.

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

Function to upload image

protected void btnArticlePageImage_Click(object sender, EventArgs e)
{

    try
    {
        String filePath = string.Empty;
        String CurrentGUID = Guid.NewGuid().ToString();

        if (FileUpload2.HasFile)
        {
            string filename = System.IO.Path.GetFileName(FileUpload2.FileName);
            System.IO.FileInfo f = new System.IO.FileInfo(FileUpload2.PostedFile.FileName);
            double fileSize = (double)FileUpload2.FileBytes.Length;
            if (fileSize < 1024000) // 1 MB current size size in bytes 102400=100kb  512000 = 500kb
            {
                if ((f.Extension.ToLower() == ".jpg") || (f.Extension.ToLower() == ".png") || (f.Extension.ToLower() == ".gif") || (f.Extension.ToLower() == ".jpeg"))
                {
                    filename = CurrentGUID + f.Extension;
                    //string productGUID 
                    filePath = Server.MapPath("../ImagesArticles/") + filename;
                    if (System.IO.File.Exists(filePath))
                    {
                        return;
                    }
                    else
                    {
                        //Upload files
                        FileUpload2.PostedFile.SaveAs(Server.MapPath("../ImagesArticles/") + filename);
                        //objPages.PageBannerImageEn = filename;
                        Session["ArticleLargeImage"] = filename.ToString();
                        string errMsg = "File Uploaded Successfully";
                        lblImageUploadMessage1.Text = errMsg;
                        // ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
                        Helper.ResizeImage(filePath, filePath, 150, 80, true);
                    }
                    return;
                }
                else
                {
                    string errMsg = "File must be an Image type of .jpg, .png, .gif, .jpeg";
                    //client-side error
                    lblImageUploadMessage1.Text = errMsg;
                    return;
                }
            }
            else
            {
                string errMsg = "File size is greater the 1MB";
                //client-side error
                lblImageUploadMessage1.Text = errMsg;
                return;
            }
        }
        else
        {
            //lblMesg.Text = "Only type .jpg, .png, .gif are allow";
            string errMsg = "Cant Upload File due to some error";
            //client-side error
            lblImageUploadMessage1.Text = errMsg;
            return;
        }
    }
    catch (Exception ex)
    {
        Response.Write("ERROR MESSAGE : " + ex.Message.ToString());
    }

}

Sequence: User can upload image type JPG, GIF, PNG etc. i only rename the image with GUID and keep the image extension same and then save it on the web server. After that i re size the image for later use.

These re sized images work fine in all browsers but only issue is if someone downloads these image for editing it wont open in any graphics editor tools.

It gives a common error message as mentioned in the first part of question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2025-01-11 17:13:55

尝试添加要保存的图像格式,这是针对 gif 图像的

NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Gif);

Try adding the imageformat to save, this is for a gif image

NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Gif);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文