Click here to Skip to main content
Rate this: bad
good
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 18:20pm
Edited 25-Feb-11 19:06pm
Albin Abel19.8K
v2

1 solution

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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 26 Feb 2011
Copyright © CodeProject, 1999-2014
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