从文件中读取BMP图像并将其转换为VB.NET中的数组 [英] Reading BMP image from file and convert it into array in VB.NET

查看:42
本文介绍了从文件中读取BMP图像并将其转换为VB.NET中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要代码来从文件中读取图像并将图像转换为整数数组.图像格式为 BMP,我使用的是 vb.net-2010

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

推荐答案

您可以在以下位置找到类似的问题和有价值的答案(虽然问题和答案是针对 c# 我认为它们会帮助您理解解决方案):如何将图像像素的值读取为 RGB成二维数组?

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?

首先,您需要将文件加载到 System.Drawing.Bitmap 对象.然后您可以使用 GetPixel 方法读取像素值.请注意,每个像素数据都包含一个颜色值.您可以使用 ToArgb() 方法将此值转换为整数值.

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

以及将 2D 数组存储到 BMP 对象的情况(假设您有一个 100x100 的 2D 数组 imageArray)

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

这篇关于从文件中读取BMP图像并将其转换为VB.NET中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆