只会保存调整大小的图像

发布于 2024-09-03 11:22:55 字数 3703 浏览 2 评论 0原文

我希望它只保存调整大小的图像,所以尝试这样做。它在上传后再次删除原始图像,但不能,因为它说该图像正在被另一个进程使用。请帮忙。
无法简单地删除存储原始文件的位置,因为它使用它来调整大小。

我使用此代码来保存文件:

string tempPath = "Galleryt";
string imgPath = "Gallery";
string savePath =  Path.Combine(Request.PhysicalApplicationPath, tempPath);
string imgSavePath = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string imgSavePath2 = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string ProductImageNormal = Path.Combine(imgSavePath, imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp = Path.Combine(savePath, "t__" + imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp2 = Path.Combine(imgSavePath2, "b__" + imageName + Fileupload1.PostedFile.FileName);
string extension = Path.GetExtension(Fileupload1.PostedFile.FileName);

switch (extension.ToLower())
{
    case ".png": goto case "Upload";
    case ".gif": goto case "Upload";
    case ".jpg": goto case "Upload";
    case "Upload": Fileupload1.PostedFile.SaveAs(ProductImageNormal);
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp, 250, 350, true, "heigh");
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp2, 600, 600, true, "heigh");

        Label1.Text = "";
        break;
    default:
        Label1.Text = "Status: Denne filtype er ikke tilladt";
        return;

}

如果我尝试使用

   File.Delete(Server.MapPath("~/Gallery/" + imageName + Fileupload1.PostedFile.FileName));

GenerateThumbnail方法的代码删除原始文件

  public static void GenerateThumbnail(string filePath, string OriginalFile, int width, int height, bool retainAspect, string quality)
{
    Bitmap bitmapNew;
    float fx, fy, f;
    int widthTh, heightTh;
    int widthOrig, heightOrig;
    bitmapNew = new Bitmap(filePath);

    if (retainAspect)
    {
        widthOrig = bitmapNew.Width;
        heightOrig = bitmapNew.Height;
        fx = widthOrig / width;
        fy = heightOrig / height;
        f = Math.Max(fx, fy);

        if (f<1)
        {
            f=1;
        }
        widthTh = (int)(widthOrig/f);
        heightTh = (int)(heightOrig/f);
    }
    else
    {
        widthTh = width;
        heightTh = height;


    }

    Size newSize = new Size(widthTh, heightTh);

    using(Bitmap thumb = new Bitmap((System.Drawing.Image)bitmapNew, newSize))
    {
        Graphics g = Graphics.FromImage(thumb);
        Int64 qualityLevel = 25L;

        if (quality == "high")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
             qualityLevel = 95L;
        }
        if (quality == "medium" || quality == "low")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            if (quality =="medium")
                qualityLevel = 65L;

        }
        System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
        System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
        eParams.Param[0]=new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityLevel);
        //Her genereres den nye thumbnail
        thumb.Save(OriginalFile, codec, eParams);
        thumb.Dispose();
    }
}

I want it to only saves the resized images so have tried to do so. It deletes the original image again after it has been uploaded but cannot because it says the image is being used by another process. Please help.
Cannot simply remove where it stores the original because it uses it to resize.

I use this code to save the files:

string tempPath = "Galleryt";
string imgPath = "Gallery";
string savePath =  Path.Combine(Request.PhysicalApplicationPath, tempPath);
string imgSavePath = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string imgSavePath2 = Path.Combine(Request.PhysicalApplicationPath, imgPath);
string ProductImageNormal = Path.Combine(imgSavePath, imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp = Path.Combine(savePath, "t__" + imageName + Fileupload1.PostedFile.FileName);
string ProductImagetemp2 = Path.Combine(imgSavePath2, "b__" + imageName + Fileupload1.PostedFile.FileName);
string extension = Path.GetExtension(Fileupload1.PostedFile.FileName);

switch (extension.ToLower())
{
    case ".png": goto case "Upload";
    case ".gif": goto case "Upload";
    case ".jpg": goto case "Upload";
    case "Upload": Fileupload1.PostedFile.SaveAs(ProductImageNormal);
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp, 250, 350, true, "heigh");
        ImageTools.GenerateThumbnail(ProductImageNormal, ProductImagetemp2, 600, 600, true, "heigh");

        Label1.Text = "";
        break;
    default:
        Label1.Text = "Status: Denne filtype er ikke tilladt";
        return;

}

if i try to delete the original file just after with code

   File.Delete(Server.MapPath("~/Gallery/" + imageName + Fileupload1.PostedFile.FileName));

the method of GenerateThumbnail

  public static void GenerateThumbnail(string filePath, string OriginalFile, int width, int height, bool retainAspect, string quality)
{
    Bitmap bitmapNew;
    float fx, fy, f;
    int widthTh, heightTh;
    int widthOrig, heightOrig;
    bitmapNew = new Bitmap(filePath);

    if (retainAspect)
    {
        widthOrig = bitmapNew.Width;
        heightOrig = bitmapNew.Height;
        fx = widthOrig / width;
        fy = heightOrig / height;
        f = Math.Max(fx, fy);

        if (f<1)
        {
            f=1;
        }
        widthTh = (int)(widthOrig/f);
        heightTh = (int)(heightOrig/f);
    }
    else
    {
        widthTh = width;
        heightTh = height;


    }

    Size newSize = new Size(widthTh, heightTh);

    using(Bitmap thumb = new Bitmap((System.Drawing.Image)bitmapNew, newSize))
    {
        Graphics g = Graphics.FromImage(thumb);
        Int64 qualityLevel = 25L;

        if (quality == "high")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
             qualityLevel = 95L;
        }
        if (quality == "medium" || quality == "low")
        {
             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
             g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            if (quality =="medium")
                qualityLevel = 65L;

        }
        System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];
        System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
        eParams.Param[0]=new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityLevel);
        //Her genereres den nye thumbnail
        thumb.Save(OriginalFile, codec, eParams);
        thumb.Dispose();
    }
}

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

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

发布评论

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

评论(2

九厘米的零° 2024-09-10 11:22:55

问题出在我们看不到的代码中,GenerateThumbnail()。猜测它可能的工作方式,它从 ProductImageNormal 创建一个位图。这会对该文件加锁,以防止其被删除。您必须调用位图的 Dispose() 方法来释放锁。

The problem is located in code we cannot see, GenerateThumbnail(). Guessing at the way it might work, it creates a bitmap from ProductImageNormal. That puts a lock on the file which prevents it from being deleted. You'll have to call the bitmap's Dispose() method to release the lock.

我是有多爱你 2024-09-10 11:22:55

如果 ImageTools.GenerateThumbnail 方法可以采用 Stream,请考虑将 PostedFile 写入 [MemoryStream][1] 并在保存缩略图之前将其传递给此方法,甚至使用 PostedFile.InputStream 。这样,原始图像仅存在于内存中,您甚至不需要将其保存到磁盘。

If the ImageTools.GenerateThumbnail method can take a Stream, consider writing the PostedFile to a [MemoryStream][1] and passing that to this method before saving the thumbnails, or even using PostedFile.InputStream directly if possible. This way the original image is only ever in memory and you do not even save it to disk.

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