Click here to Skip to main content
12,403,699 members (74,167 online)
Rate this:
 
Please Sign up or sign in to vote.
* I think VB.Net is not support Pointer.
Someone please tell me any other way then.
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 25-Feb-11 17:20pm
Updated 25-Feb-11 18:06pm
Albin Abel20.2K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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
  Permalink  
Comments
pwasser 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 Jul 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100