How to get Integer array from Bitmap (RGB)

Mansour_Dalir 1,856 Reputation points
2023-09-21T17:24:51.3533333+00:00
       
        Dim bm As Bitmap = New Bitmap(Me.Width - 2, Me.Height - 2)'or Any Picture
        Using gr As Graphics = Graphics.FromImage(bm)
            gr.CopyFromScreen(
                Me.Left + 1,
               Me.Top + 1,
                0, 0,
               bm.Size,
                CopyPixelOperation.SourceCopy)
        End Using
        frmPixcelInfo.BackgroundImageLayout = ImageLayout.None
        frmPixcelInfo.BackgroundImage = bm
        frmPixcelInfo.Refresh()
    
         Dim rec As New Rectangle(0, 0, bm.Width, bm.Height)
        Dim bmpData As System.Drawing.Imaging.BitmapData = bm.LockBits(rec, Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat)
        Dim ptr As IntPtr = bmpData.Scan0
        Dim bytes As Integer = Math.Abs(bmpData.Stride) * bm.Height
        Dim rgbValues(bytes - 1) As Byte
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
        Dim stt6 As String = String.Join(",", rgbValues) ' System.Text.Encoding.Unicode.GetString(rgbValues)
        frmPixcelInfo.txtPixcels.Text = stt6 ' out is byte,byte,byte byte ARGB,  need all ARGB to integer
        bm.UnlockBits(bmpData) 'thank
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 31,011 Reputation points Microsoft Vendor
    2023-09-22T07:20:24.96+00:00

    Hi @Mansour_Dalir ,

    Please check if the following code helps.

    Dim bm As Bitmap = New Bitmap(Me.Width - 2, Me.Height - 2)
    Using gr As Graphics = Graphics.FromImage(bm)
        gr.CopyFromScreen(
            Me.Left + 1,
            Me.Top + 1,
            0, 0,
            bm.Size,
            CopyPixelOperation.SourceCopy)
    End Using
    
    Dim rec As New Rectangle(0, 0, bm.Width, bm.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = bm.LockBits(rec, Imaging.ImageLockMode.ReadOnly, bm.PixelFormat)
    Dim ptr As IntPtr = bmpData.Scan0
    Dim bytes As Integer = Math.Abs(bmpData.Stride) * bm.Height
    Dim argbValues(bytes / 4 - 1) As Integer
    
    System.Runtime.InteropServices.Marshal.Copy(ptr, argbValues, 0, argbValues.Length)
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,361 Reputation points
    2023-09-21T17:34:41.5966667+00:00

    Don't remember where I found this but here you go,

        Public Shared Function ConvertToByteArray(ByVal value As Bitmap) As Byte()
            Dim bitmapBytes() As Byte
            Using stream As New System.IO.MemoryStream
                value.Save(stream, value.RawFormat)
                bitmapBytes = stream.ToArray
            End Using
            Return bitmapBytes
        End Function
    
    
    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.