【问题标题】:Brightness/exposure function with vb .netvb .net的亮度/曝光功能
【发布时间】:2009-08-13 01:46:33
【问题描述】:

我正在VB.NET中制作一个简单的图像编辑器,其中一个功能是亮度/曝光。

这就是我的做法:

For i = 0 To img.Width - 1
    For j = 0 To img.Height - 1
        Dim s As Color = img.GetPixel(i, j)

        Dim r As Integer = s.R * 2
        Dim g As Integer = s.G * 2
        Dim b As Integer = s.B * 2
        If s.R * 2 > 255 Then
            r = 255
        End If

        If s.G * 2 > 255 Then
            g = 255
        End If
        If s.B * 2 > 255 Then
            b = 255
        End If
        Dim x As Color = Color.FromArgb(255, r, g, b)
        img.SetPixel(i, j, x)

    Next
Next

其中 2 是亮度,亮度是原来的两倍。

唯一的问题是这似乎效果不佳,因为它确实做到了,但需要大约 30 秒! 我究竟做错了什么?有没有更好的实现方式?

谢谢

【问题讨论】:

    标签: vb.net functional-programming brightness


    【解决方案1】:

    您可以使用 ColorMatrix 和 ImageAttributes 来加快速度。方法如下:

    Imports System.Drawing.Imaging
    
    Public Class Form1
    
    Dim g As Graphics
    Dim img As Image
    Dim r As Rectangle
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      ' load photo file into picture box and initialize graphics
      img = Image.FromFile("c:\tmp.jpg")
      PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height, PixelFormat.Format32bppArgb)
      g = Graphics.FromImage(PictureBox1.Image)
      r = New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height)
      g.DrawImage(img, r)
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Call setBrightness(0.2)
    End Sub
    
    Sub setBrightness(ByVal Brightness As Single)
      ' Brightness should be -1 (black) to 0 (neutral) to 1 (white)
    
      Dim colorMatrixVal As Single()() = { _
         New Single() {1, 0, 0, 0, 0}, _
         New Single() {0, 1, 0, 0, 0}, _
         New Single() {0, 0, 1, 0, 0}, _
         New Single() {0, 0, 0, 1, 0}, _
         New Single() {Brightness, Brightness, Brightness, 0, 1}}
    
      Dim colorMatrix As New ColorMatrix(colorMatrixVal)
      Dim ia As New ImageAttributes
    
      ia.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
    
      g.DrawImage(img, r, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia)
      PictureBox1.Refresh()
    End Sub
    
    End Class
    

    【讨论】:

      【解决方案2】:

      使用以下示例,应该很容易组合一个更改图像亮度/对比度和其他特征的项目。

      这是一个使用 ColorMatrix 来更改图像的示例,它应该更快:

      http://www.developerfusion.com/forum/thread/35125/

      这是一个同时做亮度和对比度的例子:

      http://www.codeproject.com/KB/graphics/multiple_color_matrices.aspx

      另一个使用 ColorMatrix 的例子:

      http://www.codeproject.com/KB/GDI-plus/colormatrix.aspx

      并供在 ColorMatrix 上参考:

      http://msdn.microsoft.com/en-us/library/ms534063%28VS.85%29.aspx

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 2016-03-20
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多