怎样把修改后的图象保存到原图?

如果我们打开一幅图象,修改这幅图象后,然后用与原图象相同的格式将文件保存在原来的路径上或者换一种格式来保存!这时,系统会提示我们“GDI+发生一般性错误”,这个问题是怎样产生的呢?
这个问题的产生,我们可以简单的说明下:
这是因为GDI+在读取图象文件时只是读取该图象文件的头部分!这样,你能了解这幅图的信息,包括该文件图象的宽度、高度和颜色深度信息等.在整个图象文件的生存周期中,总是保持对此图象文件处于打开状态,并且锁定此状态不让别的进程来试图再打开此文件。这样,你不能用修改过的图象来替换原文档!
那么,为了将修改过的图象写回原路径,你必须释放原图象的资源,否则保存图象时候将会出错!
具体过程如下:
1、打开图象文件
2、在内存中创建一个和原图象同样大小的位图文件
3、利用这个位图文件建立Graphics对象
4、把欲处理的图象绘制到位图文件上
5、释放图象文件所占用的资源
6、用Graphics的功能修改位图文件
7、释放Graphics对象资源
8、将位图文件以你所想要的格式保存
下述程序实现了文件格式转化功能,并以给图象增加时间戳来验证这一技术。程序设计界面如下:
                                  
使用方法:
1、单击 "Choose files"以选择一幅或者多幅图象文件。
2、在下拉框中选择文件保存格式!
3、确定是否给打开的图象文件加时间戳,然后单击"Convert" 命令按狃!
说明:如果你选择了JPEG格式的文件,并加了时间戳,并选择保存文件为JPEG格式,那么这将对原图象进行修改(加了个时间戳),注意不要对你想要保留的文件进行上述操作!
实现上述功能的代码如下:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace FormatConvertor
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.CheckBox checkBox1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    public Form1()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.comboBox1 = new System.Windows.Forms.ComboBox();
      this.button2 = new System.Windows.Forms.Button();
      this.label1 = new System.Windows.Forms.Label();
      this.checkBox1 = new System.Windows.Forms.CheckBox();
      this.SuspendLayout();
      //
      // button1
      //
      this.button1.Location = new System.Drawing.Point(8, 24);
      this.button1.Name = "button1";
      this.button1.TabIndex = 0;
      this.button1.Text = "Choose files";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      //
      // comboBox1
      //
      this.comboBox1.Items.AddRange(new object[] {
                               "GIF",
                               "JPG",
                               "JPEG",
                               "BMP",
                               "PNG"});
      this.comboBox1.Location = new System.Drawing.Point(96, 24);
      this.comboBox1.Name = "comboBox1";
      this.comboBox1.Size = new System.Drawing.Size(121, 21);
      this.comboBox1.TabIndex = 1;
      //
      // button2
      //
      this.button2.Location = new System.Drawing.Point(232, 24);
      this.button2.Name = "button2";
      this.button2.TabIndex = 0;
      this.button2.Text = "Convert";
      this.button2.Click += new System.EventHandler(this.button2_Click);
      //
      // label1
      //
      this.label1.Location = new System.Drawing.Point(96, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(100, 16);
      this.label1.TabIndex = 2;
      this.label1.Text = "Output format";
      //
      // checkBox1
      //
      this.checkBox1.Location = new System.Drawing.Point(8, 64);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(208, 24);
      this.checkBox1.TabIndex = 3;
      this.checkBox1.Text = "Time stamp the image.";
      //
      // Form1
      //
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(312, 93);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.checkBox1,
                                      this.label1,
                                      this.comboBox1,
                                      this.button1,
                                      this.button2});
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
      this.Name = "Form1";
      this.Text = "Image Format Converter";
      this.ResumeLayout(false);
   }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.Run(new Form1());
    }
    string[] names;
    bool chosen=false;
    private void button1_Click(object sender, System.EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();
      dlg.Filter="Image files (*.jpg,*.jpeg,*.bmp,*.gif,*.png)|*.BMP;*.PNG;*.JPG;*.JPEG;*.PNG";
      dlg.Multiselect=true;
      if(dlg.ShowDialog()==DialogResult.OK)
      {
        chosen=true;
        names=dlg.FileNames;
      }
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
       if(chosen && this.comboBox1.Text!="")
      {
        chosen=false;
        foreach(string s in names)
        {
          //open the file
          Image i = Image.FromFile(s);
          //create temporary
          Image t=new Bitmap(i.Width,i.Height);
          //get graphics
          Graphics g=Graphics.FromImage(t);
          //copy original
          g.DrawImage(i,0,0);
          //close original
          i.Dispose();
          if(this.checkBox1.Checked==true)
          {
            //draw on temporary
            Font f = new Font("Verdana",30);
            g.DrawString(DateTime.Now.ToShortDateString()+":"+DateTime.Now.ToShortTimeString(),
              f,
              Brushes.Red,
              10,10,
              StringFormat.GenericTypographic);
            f.Dispose();
          }
          g.Dispose();
          ImageFormat fmt = ImageFormat.Bmp;
          switch(this.comboBox1.Text)
          {
            case "JPG":
            case "JPEG":
              fmt=ImageFormat.Jpeg;
              break;
            case "GIF":
              fmt=ImageFormat.Gif;
              break;
            case "PNG":
              fmt=ImageFormat.Png;
              break;
         }
          //save the file. Even if its the same filename
          t.Save(Path.GetFileNameWithoutExtension(s)+"."+this.comboBox1.Text,fmt); 
        }
      }
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【项目介绍】 基于MATLAB实现传统图像去噪算法和深度学习DnCNN模型图像去噪源码+项目说明.zip 该资源内项目代码都是经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶, 或者实际项目借鉴参考! 当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。如果基础还行,也可在此代码基础上进行修改,以实现其他功能。 1. 项目介绍 # 1.1 项目的背景 该项目是为了研究基于深度卷积神经网络的图像去噪算法,是利用DnCNN模型,但是为了比较该算法的效果,另外实现了四种传统的图像去噪算法(均值滤波、中值滤波、非局部均值滤波NLM和三维块匹配滤波BM3D)作为对照组。 # 1.2 噪声强度和类型 项目中实现五种算法对噪声强度为10,15,20...60,65,70的高斯白噪声进行处理。 # 1.3 评价指标 图像去噪后,如何评估算法去噪效果的好坏呢?项目中采用峰值信噪比PSNR和结构相似性SSIM作为评价指标。一般来说,PSNR越大,去噪效果越好。SSIM取值为0到1,越接近1,表示效果越好。 2. 数据集介绍 该项目中只是对Set12数据集进行处理,也就是项目中的Set12目录下的12张图片。如果觉得数据量不够充分,可以自行添加其他数据集,在代码中修改一下数据集的目录即可。 3. 代码介绍 对于均值滤波、中值滤波、和NLM,MATLAB都已经实现了,所以我们直接调用MATLAB自带的函数就可以。 BM3D和DnCNN的代码都是从别人那儿clone下来,做了一些小的修改。 五种算法都是对Set12数据集进行去噪,去噪的结果并没有保存,只是在运行过程中能看到去噪和去噪后的图像对比,感兴趣的朋友可以自己将图像保存下来观察。 4. 代码运行 五种算法分别在五个不同的目录中,所以你只需要进行对应的目录,运行代码即可。 + 均值滤波、中值滤波、NLM算法对应的目录分别为avefilter、medainfilter、nlm-image-denoising。每个目录下只有一个.m文件,所以只需要运行对应的文件即可。 + BM3D对应的目录是BM3D,运行该目录下的main.m程序即可。 + DnCNN对应的目录是DnCNN,运行该目录下的Demo_test_DnCNN.m程序即可,该算法目录中对应的还有好几个代码,都是原项目中有的,我没有动过,感兴趣的朋友可以自己看看。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值