0

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

Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53

1 Answers1

1

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
...
Gokhan
  • 441
  • 2
  • 9
  • plz ...If I want to execute storing a 2D array as a BMP image file ? How can I do? – dyar mohammad Jan 01 '19 at 16:02
  • The compiler give this error......Error Argument not specified for parameter 'y' of 'Public Sub SetPixel(x As Integer, y As Integer, color As System.Drawing.Color)'. ..... How can I solve the problem? Gokhan – dyar mohammad Jan 02 '19 at 18:31
  • Excuse me that line should be : img.SetPixel(i,j,Color.FromArgb(imageArray(i,j))) Corrected in answer – Gokhan Jan 02 '19 at 19:17