📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
Private i, j, x, y As Integer
Private rgbpoint(1024, 768) As rgbp
Private Structure rgbp
Dim r As Integer
Dim g As Integer
Dim b As Integer
End Structure
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(176, 264)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'PictureBox2
'
Me.PictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox2.Location = New System.Drawing.Point(280, 8)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(176, 264)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox2.TabIndex = 1
Me.PictureBox2.TabStop = False
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(192, 128)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "转换为灰度"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(192, 88)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 3
Me.Button1.Text = "打开图片"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(192, 168)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 5
Me.Button3.Text = "关闭窗口"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(464, 289)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.PictureBox2)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "彩色图片转换成灰度图片"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub getrgbp()
Dim tempcolor As Color
Dim newbitmap As Bitmap = Me.PictureBox1.Image
x = newbitmap.Width
y = newbitmap.Height
For i = 0 To x - 1 Step 1
For j = 0 To y - 1 Step 1
tempcolor = newbitmap.GetPixel(i, j)
rgbpoint(i, j).r = tempcolor.R
rgbpoint(i, j).g = tempcolor.G
rgbpoint(i, j).b = tempcolor.B
Next
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim temprgb As rgbp
Dim newbitmap As Bitmap
Dim tempcolor As Color
newbitmap = Me.PictureBox1.Image.Clone
Call getrgbp()
For i = 1 To x - 1 Step 1
For j = 1 To y - 1 Step 1
temprgb.r = rgbpoint(i, j).r + rgbpoint(i, j).g + rgbpoint(i, j).b
temprgb.g = temprgb.r
temprgb.b = temprgb.r
temprgb.r = temprgb.r \ 3
temprgb.g = temprgb.g \ 3
temprgb.b = temprgb.b \ 3
newbitmap.SetPixel(i, j, _
Color.FromArgb(temprgb.r, temprgb.g, temprgb.b))
Next
Next
PictureBox2.Image = newbitmap
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim opendia As OpenFileDialog
opendia = New OpenFileDialog
opendia.ShowDialog()
Dim newbitmap As Bitmap
Try
Me.PictureBox1.Image = New Bitmap(opendia.FileName)
Catch
Me.PictureBox1.Image = New Bitmap("..\黄山云海.bmp")
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -