使用位图的保存方法覆盖图像

发布于 2024-07-25 17:58:44 字数 705 浏览 4 评论 0原文

我有一个 ASP.NET C# 页面,我正在其中调整文件夹中图像的大小。 我正在使用 GDI+ 来执行此操作。 我想调整图像大小并替换为旧图像。

因此,当我尝试使用现有名称保存时,Save 方法会抛出错误。 但如果我给出不同的名称,它就会被保存。 但我希望新创建的调整大小的图像具有相同的文件名,因为我需要用调整大小的新文件覆盖现有文件。 我怎样才能继续前进?

我的代码是:

     oldImagePath= oldImagePath.Replace(".jpg", "NEW.jpg");
        
        try
        {
            ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
            EncoderParameters Params = new EncoderParameters(1);
            Params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
            target.Save(oldImagePath, Info[1], Params);
        }

如果我注释为目标文件创建新名称的第一行,它将不起作用,否则它将起作用。 但我想要同名。 我怎样才能做到这一点?

I have an ASP.NET C# page where I am resizing the images in a folder. I am using GDI+ to do this. I want to resize the images and replace with the old images.

So when I am trying to save with the existing name, Save method is throwing an error. But if I give a different name it is getting saved. But I want to have the same file name for the newly created resized image as I need to overwrite the existing file with the new file which is resized. How can I go ahead?

My code is:

     oldImagePath= oldImagePath.Replace(".jpg", "NEW.jpg");
        
        try
        {
            ImageCodecInfo[] Info = ImageCodecInfo.GetImageEncoders();
            EncoderParameters Params = new EncoderParameters(1);
            Params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
            target.Save(oldImagePath, Info[1], Params);
        }

If I comment the first line which creates a new name for the destination file, it will not work, otherwise it will. But I want to have the same name. How can I achieve that?

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

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

发布评论

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

评论(2

旧城空念 2024-08-01 17:58:44

如果您使用以下行中的技术加载图像

 Image imgPhoto = Image.FromFile("myphoto.jpg");

,那么这将使该文件的句柄保持打开状态,因此当您尝试覆盖它时,该文件当前仍在使用中,因此您无法写入它。

为了解决这个问题,如果将文件加载到流中,则可以覆盖原始文件,因为图像信息已写入内存,文件句柄已被释放。

您可以通过以下方式执行此操作:

FileStream fs = new FileStream("myphoto.jpg", FileMode.Open);
Image imgPhoto = Image.FromStream(fs);
fs.Close();

If you load an image using the technique in the following line

 Image imgPhoto = Image.FromFile("myphoto.jpg");

then this keeps a handle open to the file so when you attempt to overwrite it the file is still currently in use and so you are unable to write to it.

To get around this, if you load the file into a stream then this allows you to overwrite the original file as the file handle has been freed as the image information has been written to memory.

You can do this in the following way:

FileStream fs = new FileStream("myphoto.jpg", FileMode.Open);
Image imgPhoto = Image.FromStream(fs);
fs.Close();
还在原地等你 2024-08-01 17:58:44

您需要先删除原始文件。 区分这一点很重要 - 当您在 .NET 中进行图像操作时,您正在使用一个内存中对象,该对象的字节是通过读取原始图像来填充的。 您没有使用实际的原始图像。 因此,当您去保存这个全新的对象(恰好使用现有图像中的数据)并且尝试使用已在使用的路径时,您将收到异常。

您还需要确保此时原始文件尚未打开; 确保处理掉用于填充正在操作的 Image 对象的原始文件流。 然后删除,然后保存。

You need to delete the original file first. It is important to make the distinction - when you are working with image manipulation in .NET, you are working with an in-memory object whose bytes were populated by reading the original image. You are not working with the actual original image. So when you go to save this entirely new object (which happens to use data from an existing image), and you try to use an already in-use path, you will get an exception.

You also need to make sure the original file is not still open at this point; make sure to dispose of the original file stream you used to populate the Image object you're manipulating. Then delete, then save.

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