Reading BMP image from file and convert it into array in VB.NET

后端 未结 1 354
青春惊慌失措
青春惊慌失措 2021-01-26 04:27

I need code to read an image from a file and convert the image into an array of integers. The format of image is BMP and I\'m using vb.net-2010

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 04:48

    You can find a similar question and valuable answers (although the question and answers are for c# i think they will help you to understand the solution) at : How can I read image pixels' values as RGB into 2d array?

    First you need to load the file to a System.Drawing.Bitmap object. Then you can read pixel values using GetPixel method. Note that every pixel data includes a Color value. You can convert this value to an integer value using ToArgb() method.

    Imports System.Drawing;
    ...
    
    Dim img As New Bitmap("C:\test.JPG")
    Dim imageArray (img.Width, img.Height) As Integer   
    Dim i, j As Integer
    For i = 0 To img.Width
       For j = 0 To img.Height
          Dim pixel As Color = img.GetPixel(i,j)
          imageArray (i,j) = pixel.ToArgb()
       Next j
    Next i
    ...
    

    and the case storing a 2D array to a BMP object(Assuming you have a 100x100 2D array imageArray)

    Imports System.Drawing;
    ...
    
    Dim img As New Bitmap(100,100)
    Dim i, j As Integer
    For i = 0 To img.Width
       For j = 0 To img.Height
          img.SetPixel(i,j,Color.FromArgb(imageArray(i,j)))
       Next j
    Next i
    ...
    

    0 讨论(0)
提交回复
热议问题