Pixel manipulation written in csharp needs more than an automatic conversion. I need some help to convert.

Štefan Mihael Rihar 181 Reputation points
2022-07-10T18:16:20.977+00:00

Hello from Baden Lower Austria.
Researching about manipulating pixels with visual studio visual basic dot net i stubled upon a youtube media file with the link C# Events & Delegates Part 2

I am used to type on my keyboard visual basic dot net lines. The above sample in the media file is typed in csharp. The files that are used in the video have been typed and i'll attache them to this post.

FileOperations.cs
ImageManipulation.cs
Form1.cs

I have not found an easy way to convert the files from csharp to vb dot net. I have tried some lines with a site name telerik but the possibility was not suited for all cases.

I managed to use the two files FileOperations.vb and Form1.vb
Form1.vb
FileOperations.vb

If someone could be generous and convert all three csharp files to vb dot net. If someone who can do that is able to be paid tell a price from 2 two 10 Euros.
Have a nice weekend until the next one.

Developer technologies | VB
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2022-07-11T16:41:57.993+00:00

    A basic test sample with LockBits to convert pixels of an image into gray (with well known formula), with 2 buttons (Open + Convert) and 2 PictureBoxes
    I loop on pixels on Height & Width of the image, simpler to understand to change BGR value for each pixel =>

    Imports System.Drawing.Imaging  
    Imports System.Runtime.InteropServices  
      
    Public Class Form1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Using ofd As OpenFileDialog = New OpenFileDialog()  
                ofd.Filter = "JPEG Files (*.jpg)|*.jpg|Bitmap Files (*.bmp)|*.bmp|PNG Files (*.png)|*.png|TIFF Files (*.tif, *.tiff)|*.tif;*.tiff|All Files (*.*)|*.*"  
                If ofd.ShowDialog() = DialogResult.OK Then  
                    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage  
                    If (PictureBox1.Image IsNot Nothing) Then  
                        PictureBox1.Image.Dispose()  
                    End If  
                    PictureBox1.Image = New Bitmap(ofd.FileName)  
                End If  
            End Using  
        End Sub  
      
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            Dim bmp = CType(PictureBox1.Image, Bitmap)  
            Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)  
            Dim nBytes As Integer = bmpData.Stride * bmp.Height  
            Dim pBytesArray As Byte() = New Byte(nBytes - 1) {}  
            Marshal.Copy(bmpData.Scan0, pBytesArray, 0, nBytes)  
            For nY As Integer = 0 To bmp.Height - 1  
                For nX As Integer = 0 To bmp.Width - 1  
                    Dim nPos = nY * bmpData.Stride + nX * 4  
                    Dim b As Byte = pBytesArray(nPos), g As Byte = pBytesArray(nPos + 1), r As Byte = pBytesArray(nPos + 2), a As Byte = pBytesArray(nPos + 3)  
                    Dim nGray As Byte = Gray(r, g, b)  
                    b = nGray  
                    g = nGray  
                    r = nGray  
                    pBytesArray(nPos) = b  
                    pBytesArray(nPos + 1) = g  
                    pBytesArray(nPos + 2) = r  
                Next  
            Next  
            Marshal.Copy(pBytesArray, 0, bmpData.Scan0, nBytes)  
            bmp.UnlockBits(bmpData)  
            If (PictureBox2.Image IsNot Nothing) Then  
                PictureBox2.Image.Dispose()  
            End If  
            PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage  
            PictureBox2.Image = bmp  
        End Sub  
      
        Private Function Gray(nRed As Byte, nGreen As Byte, nBlue As Byte) As Byte  
            Return (nRed * 299 + nGreen * 587 + nBlue * 114) / 1000  
        End Function  
    End Class  
      
    

    219602-lockbits.gif


1 additional answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-07-10T18:50:32.147+00:00

    GetPixel/SetPixel is not a good method to manipulate pixels (too slow)
    You must use Bitmap.LockBits (MSDN sample in VB or C# (select language at top right...))

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.