6.18 C#Day1 P0-11
一, 绪论
WinForm是.NET开发平台中对Windows Form的一种称谓
GDI+是与.NET Framework中的图形设备接口进行交互的入口。
Graphics类提供了绘制不同图形对象的方法和属 性,而且更易于使用。
二,C#图像处理基础
2.1Bitmap类、BitmapData类和Graphics类是C#图像处理中最重要的3个类
- Bitmap类
GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色.
PixelFormat属性:返回图像的像素格式°
Palette属性:获取或设置图像所使用的顔色调色板。
Height属性和Width属性:返回图像的高度和宽度.
LockBits方法和UnlockBits方法:分别锁定和解锁系统内存中的位图像素
- BitmapData类
public BitmapData LockBits ( Rectangle rect, ImageLockMode flags, PixelFormat format );
public void UnlockBits( BitampData bitmapdata );
# UnlockBits方法使用一个由LockBits返回的类型为BitmapData的参数
Height属性:被锁定位图的高度.
Width属性:被锁定位图的宽度
ScanO属性:被锁定数组的首字节地 址。
Stride属性:步幅,也称为扫描宽度。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iztruR1m-1624030242601)(C:\Users\35661\AppData\Roaming\Typora\typora-user-images\image-20210618222211748.png)]
- Graphics 类
一个应用程序只要需要进行绘制或着色,它就必须使用 Graphics 对象。
-
openFileDialog类
Fielter,title,showHelp,showDialog()
2.2 彩色图像灰度化编程实例
namespace 图像处理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 文件名
private string curFileName;
// 图像对象
private System.Drawing.Bitmap curBitmap;
private void button1_Click(object sender, EventArgs e)
{
// OpenFileDialog
OpenFileDialog opnDlg = new OpenFileDialog();
//为图像选择一个筛选器
opnDlg.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
//设置对话框标题
opnDlg.Title = "打开图像文件";
opnDlg.ShowHelp = true;
if (opnDlg.ShowDialog() == DialogResult.OK)
{
//读取当前选中的文件名
curFileName = opnDlg.FileName;
//使用Image.FromFile 建图像对象
try
{
curBitmap = (Bitmap)Image.FromFile(curFileName);
}
catch (Exception exp)
{
//抛出异常
MessageBox.Show(exp.Message);
}
}
//对窗体进行重新绘制,这将强將执行paint事件处理程序
Invalidate();
}
private void save_Click(object sender, EventArgs e)
{
//如果没有创建图像,则退出
if (curBitmap == null)
return;
// 调用 SaveFileDialog
SaveFileDialog saveDlg = new SaveFileDialog();
//设置对话框标题
saveDlg.Title = "保存为";
//改写巳存在文件时提示用户
saveDlg.OverwritePrompt = true;
//为图像选择一个饰选器
saveDlg.Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
//启用'帮助”按钮
saveDlg.ShowHelp = true;
//如果选择了格式,则保存图像
if (saveDlg.ShowDialog() == DialogResult.OK)
{ //获取用户选择的文件名
string fileName = saveDlg.FileName;
//获取用户选择文件的扩展名
string strFilExtn = fileName.Remove(0, fileName.Length - 3);
//保存文件
switch (strFilExtn)
{
case "bmp":
// bmp格式
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); break;
case "jpg":
// Jpg格式
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case "gif":
// gif格式
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);break;
case "tif":
// tif格式
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff); break;
case "png":
// png格式
curBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); break;
default: break;
}
}
}
private void close_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
PS:当一个应用程序需要进行绘制时,它必须通过Graphics对象来执行绘制操作。
使用窗体的Paint事件,重载OnPaint方法, 使用CreateGraphics方法创建Graphics对象,及其使用Graphics类的静态方法等。
例:窗体的paint事件的PaintEventArgs属性来获取一个与窗体相关联的Graphics对象
private void Forml_Paint(object sender, PaintEventArgs e)
{
//获取Graphics对象
Graphics g = e.Graphics;
if(curBitmap != null)
//使用Drawlmage方法签制图像
// 160, 20:显示在主窗体内,图像左上角的坐标
// curBitmap. Widths cur Bitmap, Height:图像的宽度和高度
g.Drawlmage(curBitmap, 160, 20r curBitmap.Width, curBitmap•Height);
}
图像
// 160, 20:显示在主窗体内,图像左上角的坐标
// curBitmap. Widths cur Bitmap, Height:图像的宽度和高度
g.Drawlmage(curBitmap, 160, 20r curBitmap.Width, curBitmap•Height);
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0tzX8hqx-1624030242609)(C:\Users\35661\AppData\Roaming\Typora\typora-user-images\image-20210618232422007.png)]