C# 图像编程初体验

来源:互联网 发布:船舶有限元软件使用 编辑:程序博客网 时间:2024/05/16 06:40

   偶尔浏图像编程的小例子。想想自己学了这么多天,还不知道图像编程是怎么回事呢。于是,小试了一下。等功力提升再完善吧。

  

  1. namespace WindowsApplication1
  2. {
  3.     partial class Form1
  4.     {
  5.         /// <summary>
  6.         /// 必需的设计器变量。
  7.         /// </summary>
  8.         private System.ComponentModel.IContainer components = null;
  9.         /// <summary>
  10.         /// 清理所有正在使用的资源。
  11.         /// </summary>
  12.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13.         protected override void Dispose(bool disposing)
  14.         {
  15.             if (disposing && (components != null))
  16.             {
  17.                 components.Dispose();
  18.             }
  19.             base.Dispose(disposing);
  20.         }
  21.         #region Windows 窗体设计器生成的代码
  22.         /// <summary>
  23.         /// 设计器支持所需的方法 - 不要
  24.         /// 使用代码编辑器修改此方法的内容。
  25.         /// </summary>
  26.         private void InitializeComponent()
  27.         {
  28.             this.pcbA = new System.Windows.Forms.PictureBox();
  29.             this.btnLoadBmp = new System.Windows.Forms.Button();
  30.             this.pcbB = new System.Windows.Forms.PictureBox();
  31.             this.btnTurnRed = new System.Windows.Forms.Button();
  32.             this.label1 = new System.Windows.Forms.Label();
  33.             this.label2 = new System.Windows.Forms.Label();
  34.             ((System.ComponentModel.ISupportInitialize)(this.pcbA)).BeginInit();
  35.             ((System.ComponentModel.ISupportInitialize)(this.pcbB)).BeginInit();
  36.             this.SuspendLayout();
  37.             // 
  38.             // pcbA
  39.             // 
  40.             this.pcbA.Location = new System.Drawing.Point(12, 24);
  41.             this.pcbA.Name = "pcbA";
  42.             this.pcbA.Size = new System.Drawing.Size(195, 192);
  43.             this.pcbA.TabIndex = 2;
  44.             this.pcbA.TabStop = false;
  45.             // 
  46.             // btnLoadBmp
  47.             // 
  48.             this.btnLoadBmp.Location = new System.Drawing.Point(285, 235);
  49.             this.btnLoadBmp.Name = "btnLoadBmp";
  50.             this.btnLoadBmp.Size = new System.Drawing.Size(75, 23);
  51.             this.btnLoadBmp.TabIndex = 4;
  52.             this.btnLoadBmp.Text = "添加图像";
  53.             this.btnLoadBmp.UseVisualStyleBackColor = true;
  54.             this.btnLoadBmp.Click += new System.EventHandler(this.button1_Click_1);
  55.             // 
  56.             // pcbB
  57.             // 
  58.             this.pcbB.Location = new System.Drawing.Point(246, 24);
  59.             this.pcbB.Name = "pcbB";
  60.             this.pcbB.Size = new System.Drawing.Size(195, 192);
  61.             this.pcbB.TabIndex = 6;
  62.             this.pcbB.TabStop = false;
  63.             // 
  64.             // btnTurnRed
  65.             // 
  66.             this.btnTurnRed.Location = new System.Drawing.Point(366, 235);
  67.             this.btnTurnRed.Name = "btnTurnRed";
  68.             this.btnTurnRed.Size = new System.Drawing.Size(75, 23);
  69.             this.btnTurnRed.TabIndex = 8;
  70.             this.btnTurnRed.Text = "染色";
  71.             this.btnTurnRed.UseVisualStyleBackColor = true;
  72.             this.btnTurnRed.Click += new System.EventHandler(this.button2_Click_1);
  73.             // 
  74.             // label1
  75.             // 
  76.             this.label1.AutoSize = true;
  77.             this.label1.Location = new System.Drawing.Point(244, 9);
  78.             this.label1.Name = "label1";
  79.             this.label1.Size = new System.Drawing.Size(41, 12);
  80.             this.label1.TabIndex = 9;
  81.             this.label1.Text = "之后:";
  82.             // 
  83.             // label2
  84.             // 
  85.             this.label2.AutoSize = true;
  86.             this.label2.Location = new System.Drawing.Point(12, 9);
  87.             this.label2.Name = "label2";
  88.             this.label2.Size = new System.Drawing.Size(41, 12);
  89.             this.label2.TabIndex = 10;
  90.             this.label2.Text = "之前:";
  91.             // 
  92.             // Form1
  93.             // 
  94.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  95.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  96.             this.ClientSize = new System.Drawing.Size(453, 270);
  97.             this.Controls.Add(this.label2);
  98.             this.Controls.Add(this.label1);
  99.             this.Controls.Add(this.btnTurnRed);
  100.             this.Controls.Add(this.pcbB);
  101.             this.Controls.Add(this.btnLoadBmp);
  102.             this.Controls.Add(this.pcbA);
  103.             this.Name = "Form1";
  104.             this.Text = "Form1";
  105.             ((System.ComponentModel.ISupportInitialize)(this.pcbA)).EndInit();
  106.             ((System.ComponentModel.ISupportInitialize)(this.pcbB)).EndInit();
  107.             this.ResumeLayout(false);
  108.             this.PerformLayout();
  109.         }
  110.         #endregion
  111.         private System.Windows.Forms.PictureBox pcbA;
  112.         private System.Windows.Forms.Button btnLoadBmp;
  113.         private System.Windows.Forms.PictureBox pcbB;
  114.         private System.Windows.Forms.Button btnTurnRed;
  115.         private System.Windows.Forms.Label label1;
  116.         private System.Windows.Forms.Label label2;
  117.     }
  118. }

下面是实现代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using System.IO;
  10. namespace WindowsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         Bitmap image1;
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         private void button1_Click_1(object sender, EventArgs e)
  20.         {
  21.             btnTurnRed.Enabled = true;
  22.             OpenFileDialog dialog = new OpenFileDialog();
  23.             dialog.InitialDirectory = @"d:/";
  24.             dialog.Filter = "BMP 图像 (*.bmp) | *.bmp";
  25.             dialog.ShowDialog();
  26.             string file = dialog.FileName;
  27.             if (file == null)
  28.             {
  29.                 MessageBox.Show("没有选择文件");
  30.                 return;
  31.             }
  32.             
  33.             try{
  34.                 image1 = new Bitmap(file, true);
  35.                 this.pcbA.Image = image1;
  36.                 this.pcbB.Image = image1;
  37.             }
  38.             catch(ArgumentException)
  39.             {
  40.                 MessageBox.Show("请选择正确文件");
  41.             }
  42.         }
  43.         private void button2_Click_1(object sender, EventArgs e)
  44.         {
  45.             if (image1 == null)
  46.                 return;
  47.             int x, y;
  48.             // Loop through the images pixels to reset color.
  49.             for (x = 0; x < image1.Width; x++)
  50.             {
  51.                 for (y = 0; y < image1.Height; y++)
  52.                 {
  53.                     Color pixelColor = image1.GetPixel(x, y);
  54.                     Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
  55.                     image1.SetPixel(x, y, newColor);
  56.                 }
  57.             }
  58.             // Set the PictureBox to display the image.
  59.             pcbB.Image = image1;
  60.             btnTurnRed.Enabled = false;            
  61.         }
  62.     }
  63. }

 

 

 

原创粉丝点击