C#:GDI +:使用覆盖的位图保存方法图像 [英] C# :GDI+ : OverWriting an Image using Save Method of Bitmap

查看:944
本文介绍了C#:GDI +:使用覆盖的位图保存方法图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在哪里调整图像的文件夹中。我正在使用GDI +来干这事要调整图像和同当我试图挽救与老images.So替换ASP.NET C#页面现有的名称,保存方法是抛出一个错误。但是,如果我给它越来越saved.But不同的名字我想为新创建的调整后的图像相同的文件名,因为我需要覆盖现有文件,新文件这是调整大小。不知道如何继续下去。
我的code是

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. Any idea how to 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);
        }

如果我评论该为目标文件创建一个新名称的第一个行,它不会工作,其他wose它will.But我想有。任何想同一个名字? ?在此先感谢

if i comment the first line which creates a new name for the destination file, IT will not work.,other wose it will.But i want to have the same name .Any thought ?? ? Thanks in advance

推荐答案

您需要先删除原来的文件。当你与图像处理的.NET工作时,与内存中的对象,它的字节通过读取原始图像进行填充工作 - 使的区别是很重要的。你是不是有实际的原始图像的工作。所以,当你去保存这个全新的对象(这恰好从现有的图像用数据),并尝试使用已经在使用的路径,你会得到一个异常。

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.

您还需要确保原始文件是不是仍然在这一点上打开;确保处置您用来填充你操纵Image对象原始文件流。然后删除,然后保存。

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.

这篇关于C#:GDI +:使用覆盖的位图保存方法图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆