【问题标题】:Reading BMP image from file and convert it into array in VB.NET从文件中读取 BMP 图像并将其转换为 VB.NET 中的数组
【发布时间】:2018-12-28 21:00:43
【问题描述】:

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

【问题讨论】:

    标签: vb.net-2010


    【解决方案1】:

    您可以在How can I read image pixels' values as RGB into 2d array? 找到类似的问题和有价值的答案(尽管问题和答案是针对 c# 我认为它们会帮助您理解解决方案)

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

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

    以及将二维数组存储到 BMP 对象的情况(假设您有一个 100x100 二维数组 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
    ...
    

    【讨论】:

    • plz ...如果我想执行将二维数组存储为 BMP 图像文件?我该怎么办?
    • 编译器给出这个错误......错误参数没有为'Public Sub SetPixel(x As Integer,y As Integer,color As System.Drawing.Color)的参数'y'指定'。 .....我该如何解决这个问题?悟空
    • 对不起,该行应该是:img.SetPixel(i,j,Color.FromArgb(imageArray(i,j))) 已在回答中更正
    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2010-11-24
    • 2015-04-17
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多