Fast Image Processing VB.NET - Invert

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

This tutorial will cover fast image processing using a method a bit more advanced than Get/SetPixel.
So I assume that you have a bitmap called MyBitmap that we are going to edit. Also import System.Drawing, System.Drawing.Imaging and System.Runtime.InteropServices. We will have pixels with 32 bits each, so it can handle ARGB colors.
The first thing we need to do is to get the data from the bitmap. The LockBits method takes pixel data from a region in the bitmap and returns a BitmapData object with properties like Scan0; an IntPtr to the pixel data, Stride; number of bytes for each 'line', Image height, width and pixel format.
So lets get started
Code: Select all
Dim bmd As BitmapData = MyBitmap.LockBits(New Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)
This function locks the full area of the image, with read and write access, and stores the data in the 32bppArgb pixel format. It returns a BitmapData object that we need to unlock the data later.
Next we have to get hold of our pixel data.
Code: Select all
        Dim data(bmd.Stride * bmd.Height) As Byte
        Marshal.Copy(bmd.Scan0, data, 0, data.Length)
Create an array of bytes large enough to store the image and copy the data into the array. bmd.Scan0 is the start position of the image data.

Now I will show you a simple invert function. A very important thing to notice is that the data is stored in reverse. So the pixels are stored in BGRA format, with the blue value first, then green, red and alpha.
Code: Select all
        'Loop through each row of pixels'
        For y As Integer = 0 To bmd.Height - 1
            'Loop through every pixel in the row'
            For x As Integer = 0 To bmd.Width - 1
                'Remember that each pixel takes 4 bytes, or 32 bits.'

                'To invert a color, take 255, the max value, and subtract the value of the pixel'
                'y * bmd.Stride returns the current row. If you add that to x * 4, which is x * pixelsize, you get the blue byte at the x and y coordinates'
                'To get green, add 1 to the result, red add 2, and alpha you have to add 3 '
                data(y * bmd.Stride + x * 4) = 255 - data(y * bmd.Stride + x * 4) ' blue'
                data(y * bmd.Stride + x * 4 + 1) = 255 - data(y * bmd.Stride + x * 4 + 1) ' green'
                data(y * bmd.Stride + x * 4 + 2) = 255 - data(y * bmd.Stride + x * 4 + 2) ' red'

                'If you want to invert the alpha value too, uncomment the line below'
                'data(y * bmd.Stride + x * 4 + 3) = 255 - data(y * bmd.Stride + x * 4 + 3) ' alpha
            Next
        Next
The code is commented so you can see what is going on.
Finally we have to copy the data back to the image
Code: Select all
        Marshal.Copy(data, 0, bmd.Scan0, data.Length)
        MyBitmap.UnlockBits(bmd)
Now you can use view your inverted bitmap.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
1 post Page 1 of 1
Return to “Tutorials”