本项目包含用C#编程语言实现的各种图像处理算法和功能的源代码,适用于开发基于Windows的应用程序。
基于C#的典型图像处理算法第二章:
```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace gray {
public partial class Form1 : Form {
HiPerfTimer myTimer; // 假设HiPerfTimer已定义
public Form1() {
InitializeComponent();
myTimer = new HiPerfTimer();
}
private void open_Click(object sender, EventArgs e) {
OpenFileDialog opnDlg = new OpenFileDialog();
opnDlg.Filter = 所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif; +
*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf| +
位图( * . bmp ; * . jpg ; * . png ;...) | * . bmp ; * . pcx ; * . png ;* . jpg ;*. gif; +
*.tif; *.ico|矢量图(*.wmf; *.eps; *.emf;) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf;
opnDlg.Title = 打开图像文件;
opnDlg.ShowHelp = true;
if (opnDlg.ShowDialog() == DialogResult.OK) {
curFileName = opnDlg.FileName;
try {
curBitmap = (Bitmap)Image.FromFile(curFileName);
} catch (Exception exp) {
MessageBox.Show(exp.Message);
}
Invalidate();
}
}
private void save_Click(object sender, EventArgs e) {
if(curBitmap == null)
return;
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = 保存为;
saveDlg.OverwritePrompt = true;
saveDlg.Filter = BMP文件 (*.bmp) | *.bmp| +
Gif文件 (*.gif) | *.gif| +
JPEG文件 (*.jpg) | *.jpg| +
PNG文件 (*.png) | *.png;
saveDlg.ShowHelp = true;
if(saveDlg.ShowDialog() == DialogResult.OK) {
string fileName = saveDlg.FileName;
switch (fileName.Substring(fileName.Length - 3)) { // 获取后缀名
case bmp:
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); break;
case jpg:
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case gif:
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif); break;
case tif:
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff); break;
case png:
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
private void close_Click(object sender, EventArgs e) { this.Close();}
private void Form1_Paint(object sender, PaintEventArgs e){
Graphics g = e.Graphics;
if(curBitmap != null)
g.DrawImage(curBitmap, 160, 20, curBitmap.Width, curBitmap.Height);
}
private void pixel_Click(object sender, EventArgs e) { // 转换为灰度图
if (curBitmap == null)
return;
myTimer.ClearTimer(); myTimer.Start();
for(int i = 0; i < curBitmap.Width; ++i)
for(int j = 0; j < curBitmap.Height ; ++j){
Color c = curBitmap.GetPixel(i, j);
int ret = (int)(c.R * 0.299 + c.G * 0.587 + c.B * 0.114); // 计算灰度值
curBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
}
myTimer.Stop(); timeBox.Text = myTimer.Duration.ToString(####.##) + 毫秒; Invalidate();
}
private void memory_Click(object sender, EventArgs e) { // 使用内存操作
if (curBitmap == null)
return;
myTimer.ClearTimer();
myTimer.Start();
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadWrite,curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height * 3;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
for (