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

喜夏-厌秋 提交于 2019-12-02 19:27:39

问题


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:


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
...


来源:https://stackoverflow.com/questions/53964241/reading-bmp-image-from-file-and-convert-it-into-array-in-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!