总结前几节的教程内容,本节做一个综合的颜色矩阵处理图像的程序。
窗体和使用的控件如下:
txt00-txt44分别对应矩阵属性Matrix00至Matrix44。
为了简化内容,1、图片的载入为 固定图片,2、未作除错处理。
主要代码如下:
Imports System.Drawing.Imaging Public Class Form2 Dim sourceImg As Bitmap ‘载入源图片 Private Sub btnLoadimg_Click(sender As Object,e As EventArgs) Handles btnLoadimg.Click sourceImg = Image.FromFile("d:\15x.jpg") picSource.Image = sourceImg End Sub '矩阵处理 Private Sub btnDraw_Click(sender As Object,e As EventArgs) Handles btnDraw.Click Dim imageAttributes As New ImageAttributes() Dim imgMatrix As New ColorMatrix() Dim txtControl As TextBox For i As Integer = 0 To 4 For j As Integer = 0 To 4 txtControl = CType(Me.Controls("txt" & i & j),TextBox) imgMatrix.Item(i,j) = Single.Parse(txtControl.Text) Next Next imageAttributes.SetColorMatrix(imgMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap) Dim destImg As New Bitmap(sourceImg.Width,sourceImg.Height) Dim g As Graphics = Graphics.FromImage(destImg) g.DrawImage(sourceImg,New Rectangle(0,sourceImg.Width,sourceImg.Height),sourceImg.Height,GraphicsUnit.Pixel,imageAttributes) picDest.Image = destImg End Sub '重置txt00-txt44内的文本,全部设置为0 Private Sub btnReset_Click(sender As Object,e As EventArgs) Handles btnReset.Click Dim txtControl As TextBox For i As Integer = 0 To 4 For j As Integer = 0 To 4 txtControl = CType(Me.Controls("txt" & i & j),TextBox) txtControl.Text = "0" Next Next End Sub End Class
代码中需要注意的地方:
1、使用了ColorMatrix的 Items属性,这是一个二维数组:
Public Property Item ( row As Integer,column As Integer) As Single
其中row、column分别对应元素所在的行和列。
2、Me.Controls是一个控件集合,使用 Me.Controls(控件名称),可以获得该控件,但需要强制进行类型转换。
运行效果如下:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256610.html