Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
* I think VB.Net is not support Pointer.
Someone please tell me any other way then.
C#
private void contrast_function()
       {
           double nContrast = 30;
           double pixel = 0, contrast = (100.0 + nContrast) / 100.0;
           contrast *= contrast;
           int red, green, blue;
           Bitmap b = new Bitmap(pictureBox1.Image);
                     BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
           int stride = bmData.Stride;
           System.IntPtr Scan0 = bmData.Scan0;
           unsafe
           {
               byte* p = (byte*)(void*)Scan0;
               int nOffset = stride - b.Width * 3;
               for (int y = 0; y < b.Height; ++y)
               {
                   for (int x = 0; x < b.Width; ++x)
                   {
                       blue = p[0];
                       green = p[1];
                       red = p[2];
                       pixel = red / 255.0;
                       pixel -= 0.5;
                       pixel *= contrast;
                       pixel += 0.5;
                       pixel *= 255;
                       if (pixel < 0) pixel = 0;
                       if (pixel > 255) pixel = 255;
                       p[2] = (byte)pixel;
                       pixel = green / 255.0;
                       pixel -= 0.5;
                       pixel *= contrast;
                       pixel += 0.5;
                       pixel *= 255;
                       if (pixel < 0) pixel = 0;
                       if (pixel > 255) pixel = 255;
                       p[1] = (byte)pixel;
                       pixel = blue / 255.0;
                       pixel -= 0.5;
                       pixel *= contrast;
                       pixel += 0.5;
                       pixel *= 255;
                       if (pixel < 0) pixel = 0;
                       if (pixel > 255) pixel = 255;
                       p[0] = (byte)pixel;
                       p += 3;
                   }
                   p += nOffset;
               }
           }
           b.UnlockBits(bmData);
           pictureBox1.Image = (Image)b;
       }
Posted
Updated 25-Feb-11 18:06pm
v2

OBS! 'Pointer(Of xxx) is invalid. You've got to do something like:

VB
Private Sub contrast_function()
      Dim nContrast As Double = 30
      Dim pixel As Double = 0, contrast As Double = (100.0 + nContrast) / 100.0
      contrast *= contrast
      Dim red As Integer, green As Integer, blue As Integer
      Dim b As New Bitmap( pictureBox1.Image)
      Dim bmData As BitmapData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
      Dim stride As Integer = bmData.Stride
      Dim Scan0 As System.IntPtr = bmData.Scan0
      Dim buffer(stride * bmData.Height - 1) As Byte
      Dim p = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0)
      p = Scan0 'Pointer(Of Byte) = CType(CType(Scan0, Pointer(Of System.Void)), Pointer(Of Byte))
      Dim nOffset As Integer = stride - b.Width * 3
      Dim offSet = 0
      For y As Integer = 0 To b.Height - 1
          For x As Integer = 0 To b.Width - 1
              blue = buffer(0 + offSet)
              green = buffer(1 + offSet)
              red = buffer(2 + offSet)
              pixel = red / 255.0
              pixel -= 0.5
              pixel *= contrast
              pixel += 0.5
              pixel *= 255
              If pixel < 0 Then
                  pixel = 0
              End If
              If pixel > 255 Then
                  pixel = 255
              End If
              buffer(2 + offSet) = CByte(Math.Truncate(pixel))
              pixel = green / 255.0
              pixel -= 0.5
              pixel *= contrast
              pixel += 0.5
              pixel *= 255
              If pixel < 0 Then
                  pixel = 0
              End If
              If pixel > 255 Then
                  pixel = 255
              End If
              buffer(1 + offSet) = CByte(Math.Truncate(pixel))
              pixel = blue / 255.0
              pixel -= 0.5
              pixel *= contrast
              pixel += 0.5
              pixel *= 255
              If pixel < 0 Then
                  pixel = 0
              End If
              If pixel > 255 Then
                  pixel = 255
              End If
              buffer(0) = CByte(Math.Truncate(pixel))
              offSet += 3
          Next
          offSet += nOffset
      Next
      b.UnlockBits(bmData)
       pictureBox1.Image = DirectCast(b, Image)
  End Sub
 
Share this answer
 
Comments
[no name] 27-Jul-15 21:38pm    
This question is 4.5 years old. Do you think the OP is still waiting for the answer? Dragging up old questions to add solutions brings them back to the top and is considered poor form here.
Georg_S 21-Jul-17 23:05pm    
No its not of 'poor form'.
Other people googling similar questions and looking for solutions end up here - which is probably the 'main' purpose of leaving the questions on the site.
Any other solutions could help someone else long after.


VB
Private Sub contrast_function()
    Dim nContrast As Double = 30
    Dim pixel As Double = 0, contrast As Double = (100.0 + nContrast) / 100.0
    contrast *= contrast
    Dim red As Integer, green As Integer, blue As Integer
    Dim b As New Bitmap(pictureBox1.Image)
    Dim bmData As BitmapData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
    Dim stride As Integer = bmData.Stride
    Dim Scan0 As System.IntPtr = bmData.Scan0
    Dim p As Pointer(Of Byte) = CType(CType(Scan0, Pointer(Of System.Void)), Pointer(Of Byte))
    Dim nOffset As Integer = stride - b.Width * 3
    For y As Integer = 0 To b.Height - 1
        For x As Integer = 0 To b.Width - 1
            blue = p(0)
            green = p(1)
            red = p(2)
            pixel = red / 255.0
            pixel -= 0.5
            pixel *= contrast
            pixel += 0.5
            pixel *= 255
            If pixel < 0 Then
                pixel = 0
            End If
            If pixel > 255 Then
                pixel = 255
            End If
            p(2) = CByte(Math.Truncate(pixel))
            pixel = green / 255.0
            pixel -= 0.5
            pixel *= contrast
            pixel += 0.5
            pixel *= 255
            If pixel < 0 Then
                pixel = 0
            End If
            If pixel > 255 Then
                pixel = 255
            End If
            p(1) = CByte(Math.Truncate(pixel))
            pixel = blue / 255.0
            pixel -= 0.5
            pixel *= contrast
            pixel += 0.5
            pixel *= 255
            If pixel < 0 Then
                pixel = 0
            End If
            If pixel > 255 Then
                pixel = 255
            End If
            p(0) = CByte(Math.Truncate(pixel))
            p += 3
        Next
        p += nOffset
    Next
    b.UnlockBits(bmData)
    pictureBox1.Image = DirectCast(b, Image)
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900