C# 图像编程初体验
来源:互联网 发布:船舶有限元软件使用 编辑:程序博客网 时间:2024/05/16 06:40
偶尔浏图像编程的小例子。想想自己学了这么多天,还不知道图像编程是怎么回事呢。于是,小试了一下。等功力提升再完善吧。
- namespace WindowsApplication1
- {
- partial class Form1
- {
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.pcbA = new System.Windows.Forms.PictureBox();
- this.btnLoadBmp = new System.Windows.Forms.Button();
- this.pcbB = new System.Windows.Forms.PictureBox();
- this.btnTurnRed = new System.Windows.Forms.Button();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- ((System.ComponentModel.ISupportInitialize)(this.pcbA)).BeginInit();
- ((System.ComponentModel.ISupportInitialize)(this.pcbB)).BeginInit();
- this.SuspendLayout();
- //
- // pcbA
- //
- this.pcbA.Location = new System.Drawing.Point(12, 24);
- this.pcbA.Name = "pcbA";
- this.pcbA.Size = new System.Drawing.Size(195, 192);
- this.pcbA.TabIndex = 2;
- this.pcbA.TabStop = false;
- //
- // btnLoadBmp
- //
- this.btnLoadBmp.Location = new System.Drawing.Point(285, 235);
- this.btnLoadBmp.Name = "btnLoadBmp";
- this.btnLoadBmp.Size = new System.Drawing.Size(75, 23);
- this.btnLoadBmp.TabIndex = 4;
- this.btnLoadBmp.Text = "添加图像";
- this.btnLoadBmp.UseVisualStyleBackColor = true;
- this.btnLoadBmp.Click += new System.EventHandler(this.button1_Click_1);
- //
- // pcbB
- //
- this.pcbB.Location = new System.Drawing.Point(246, 24);
- this.pcbB.Name = "pcbB";
- this.pcbB.Size = new System.Drawing.Size(195, 192);
- this.pcbB.TabIndex = 6;
- this.pcbB.TabStop = false;
- //
- // btnTurnRed
- //
- this.btnTurnRed.Location = new System.Drawing.Point(366, 235);
- this.btnTurnRed.Name = "btnTurnRed";
- this.btnTurnRed.Size = new System.Drawing.Size(75, 23);
- this.btnTurnRed.TabIndex = 8;
- this.btnTurnRed.Text = "染色";
- this.btnTurnRed.UseVisualStyleBackColor = true;
- this.btnTurnRed.Click += new System.EventHandler(this.button2_Click_1);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(244, 9);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(41, 12);
- this.label1.TabIndex = 9;
- this.label1.Text = "之后:";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(12, 9);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(41, 12);
- this.label2.TabIndex = 10;
- this.label2.Text = "之前:";
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(453, 270);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.btnTurnRed);
- this.Controls.Add(this.pcbB);
- this.Controls.Add(this.btnLoadBmp);
- this.Controls.Add(this.pcbA);
- this.Name = "Form1";
- this.Text = "Form1";
- ((System.ComponentModel.ISupportInitialize)(this.pcbA)).EndInit();
- ((System.ComponentModel.ISupportInitialize)(this.pcbB)).EndInit();
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- #endregion
- private System.Windows.Forms.PictureBox pcbA;
- private System.Windows.Forms.Button btnLoadBmp;
- private System.Windows.Forms.PictureBox pcbB;
- private System.Windows.Forms.Button btnTurnRed;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- }
- }
下面是实现代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using System.IO;
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- Bitmap image1;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- btnTurnRed.Enabled = true;
- OpenFileDialog dialog = new OpenFileDialog();
- dialog.InitialDirectory = @"d:/";
- dialog.Filter = "BMP 图像 (*.bmp) | *.bmp";
- dialog.ShowDialog();
- string file = dialog.FileName;
- if (file == null)
- {
- MessageBox.Show("没有选择文件");
- return;
- }
- try{
- image1 = new Bitmap(file, true);
- this.pcbA.Image = image1;
- this.pcbB.Image = image1;
- }
- catch(ArgumentException)
- {
- MessageBox.Show("请选择正确文件");
- }
- }
- private void button2_Click_1(object sender, EventArgs e)
- {
- if (image1 == null)
- return;
- int x, y;
- // Loop through the images pixels to reset color.
- for (x = 0; x < image1.Width; x++)
- {
- for (y = 0; y < image1.Height; y++)
- {
- Color pixelColor = image1.GetPixel(x, y);
- Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
- image1.SetPixel(x, y, newColor);
- }
- }
- // Set the PictureBox to display the image.
- pcbB.Image = image1;
- btnTurnRed.Enabled = false;
- }
- }
- }
- C# 图像编程初体验
- C# 编程初体验
- C#异步编程初体验
- 【Mono】C# + Mono + Linux 编程初体验之 HelloWorld
- 【Mono】C# + Mono + Linux 编程初体验之 HelloWorld
- C#初体验
- c#初体验
- C#之初体验
- c#初体验
- C# OpenCV 初体验
- C#初体验
- c#初体验
- c# 窗体初体验
- C#初体验
- c#初体验
- C#学习 初体验
- C#体验编程技术 类与对象
- C#体验编程技术 类与对象
- 模式的威力和魅力
- 有向图的强连通分量
- 莫当IT先驱
- 你对云计算 “感冒” 吗?
- 项目总结(by Summit)
- C# 图像编程初体验
- 欧莱雅的整合之道
- 整合Spring2.5+Struts2.1+Hibernate3.2
- lua实现面向对象的特性
- 捡回的包装盒。。。1025
- 盘点电子商务10年发展路
- 企业采用SaaS的IT战略思考
- ADO.NET
- 千锤百炼的绿色PC