图片提亮速度很慢 请高手提示如何提速
经过 "greennetboy(我的老婆叫静静) "的提示 修改了代码 已经有了明显的提速 但感觉比photoshop的还是慢 要是图片是1000*1000大约需要3-5秒 时间还是比较久
我已经给了100分
在100分 请 知道朋友 在提示提示
下面代码 注释的部分是老代码 上面的是新的 提速了很多
还有没有在快的
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Me.PictureBox1.Image IsNot Nothing Then
'==================================================================================
Dim A = 10
Dim AA = (A / (127 - (A / 2)))
Dim AAA = (A / (127 + (A / 2)))
Dim bmp As Bitmap = Me.PictureBox1.Image
Dim rect As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpdata As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
Dim ptr As IntPtr = bmpdata.Scan0
Dim bytes As Integer = bmp.Width * bmp.Height * 3
Dim rgbvalues(bytes) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes)
' Me.ProgressBar1.Maximum = rgbvalues.Length
For counter As Integer = 0 To rgbvalues.Length - 1
' Me.ProgressBar1.Value = counter
If rgbvalues(counter) < 127 - A / 2 Then
rgbvalues(counter) = rgbvalues(counter) + rgbvalues(counter) * AA
Else
rgbvalues(counter) = rgbvalues(counter) + (255 - rgbvalues(counter)) * AAA
End If
If rgbvalues(counter) > 255 Then rgbvalues(counter) = 255 '输出值判断是否在0到255之间
If rgbvalues(counter) < 0 Then rgbvalues(counter) = 0
Next
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes)
bmp.UnlockBits(bmpdata)
'============================================================================
' ' 'Dim Pic As Bitmap = Me.PictureBox1.Image
' ' 'Dim intWidthLen As Integer = 0
' ' 'Dim intHeightLen As Integer = 0
' ' 'Dim Clo As Color
' ' 'Dim Nclo As Color
' ' 'Dim R, G, B As Integer
' ' 'Dim A = 10
' ' 'Dim AA = (A / (127 - (A / 2)))
' ' 'Dim AAA = (A / (127 + (A / 2)))
' ' 'intWidthLen = Me.PictureBox1.Image.Width - 1
' ' 'intHeightLen = Me.PictureBox1.Image.Height - 1
' ' 'For x As Integer = 0 To intWidthLen
' ' ' For y As Integer = 0 To intHeightLen
' ' ' Clo = Pic.GetPixel(x, y)
' ' ' With Clo
' ' ' If .R < 127 - A / 2 Then
' ' ' R = .R + .R * AA
' ' ' Else
' ' ' R = .R + (255 - .R) * AAA
' ' ' End If
' ' ' If .G < 127 - A / 2 Then
' ' ' G = .G + .G * AA
' ' ' Else
' ' ' G = .G + (255 - .G) * AAA
' ' ' End If
' ' ' If .B < 127 - A / 2 Then
' ' ' B = .B + .B * AA
' ' ' Else
' ' ' B = .B + (255 - .B) * AAA
' ' ' End If
' ' ' If R > 255 Then R = 255 '输出值判断是否在0到255之间
' ' ' If R < 0 Then R = 0
' ' ' If G > 255 Then G = 255
' ' ' If G < 0 Then G = 0
' ' ' If B > 255 Then B = 255
' ' ' If B < 0 Then B = 0
' ' ' Nclo = Color.FromArgb(R, G, B)
' ' ' Pic.SetPixel(x, y, Nclo)
' ' ' End With
' ' ' Next
' ' 'Next
Me.PictureBox1.Refresh()
End If
End Sub
[解决办法]
再快的用指针,unsafe
不过速度上提高的不是太多.你自己查查相关知识
[解决办法]
在程序中,尽量不要用到属性
Dim rgbvalues(bytes) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes)
dim intLen as integer=0
intLen =rgbvalues.Length - 1
For counter As Integer = 0 To intLen
ProgressBar1.Value = counter
If rgbvalues(counter) < 127 - A / 2 Then
rgbvalues(counter) = rgbvalues(counter) + rgbvalues(counter) * AA
Else
rgbvalues(counter) = rgbvalues(counter) + (255 - rgbvalues(counter)) * AAA
End If
If rgbvalues(counter) > 255 Then rgbvalues(counter) = 255 '输出值判断是否在0到255之间
If rgbvalues(counter) < 0 Then rgbvalues(counter) = 0
Next
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes)
bmp.UnlockBits(bmpdata)
PictureBox1.Refresh()
End If
其实代码优化可以提一点速度,关键还在于算法
[解决办法]
使用DIBSECTION可以提高一些速度.
网上有篇文章 "用VB写高效的图像处理程序 ",这里你会得到一个解决方案的,虽然是VB的但是编程是相通的,VB改VB.net不是很困难的事.网址如下
http://tech.acnow.net/Html/Program/VB/GUI/2002-3/26/224517583.shtml
里面还有其他的图像处理文章,Good Luck!