Recover data from a bitmap and filling a matrix

Recover data from a bitmap and filling a matrix

am 26.10.2007 00:53:11 von Marco Biagioni

After i've tried to update a vb 6.0 project to vb.net, using visual studio
utility,i can't read correctly data bytes from a .bmp file to insert them in
a matrix to operate on.

Using vb 6.0 the code was based on Get function:

GET #1, PIXELSTART, PHOTO.MATRIX

where PHOTO is a structure data type, with a member MATRIX previously
defined in this way:

ReDim PHOTO.MATRIX(1 TO PHOTO.WIDTH*3,1 TO PHOTO.HEIGHT)

in this way,the program read bytes starting from PIXELSTART in bmp file and
put them in transpose matrix resultant.
For example:
an image of 622*277 pixel has a matrix of 1868*277 bytes considering 24 bit
depth and so 3 bytes per pixel.

With Get function the bidimensional array,the matrix, was filled correctly,
as visible using debugger, with the single pixel value of the bitmap.

Using vb.net, the utility tansformed the code in this way:

ReDim PHOTO.MATRIX(PHOTO.WIDTH*3,PHOTO.HEIGHT)

FILEGET(1,PHOTO.MATRIX(PHOTO.WIDTH*3,PHOTO.HEIGHT),STARTPIXE L)

I know the fact that the array in enumerated in different ways in vb 6.0 and
..net(start with 0 in net,with 1 in 6.0), but the big big problem is that THE
MATRIX HERE IS FILLED WITH NOTHING!!!Every matrix value is equal to zero,
and it result in a black image!
The dimensions of the bitmap however are read correctly from bmp file in
this way:

FILEGET(1,WIDTH,19)

FILEGET(1,HEIGHT,23)

Some help please!!!

Re: Recover data from a bitmap and filling a matrix

am 26.10.2007 16:12:22 von Andrew Morton

Marco Biagioni wrote:
> FILEGET(1,PHOTO.MATRIX(PHOTO.WIDTH*3,PHOTO.HEIGHT),STARTPIXE L)

Looking at the help for FileGet, shouldn't that be

FileGet(1, PHOTO.MATRIX)

?

Andrew

Re: Recover data from a bitmap and filling a matrix

am 26.10.2007 20:39:42 von Marco Biagioni

in fileget function i must specify dimension of matrix otherwise it gives me
an error...

This is the structure:

Private Structure Photo
Dim PhotoName As String
Dim FormPhoto As frmFoto
Dim w As Integer
Dim w3 As Integer
Dim h As Integer
Dim StartPixel As Integer
Dim header() As Byte
Dim g(,) As Byte 'pixel matrix
End Structure


Then some declarations...

Dim Foto() As Photo
Dim nPhoto As Short

The load routine:

Private Sub LoadPhoto(ByVal PhotoName As String, ByVal Origin As Short)
Dim FormPhoto As Object
'carica Bitmap
Dim w As Integer
Dim h As Integer
Dim pixelOffset As Integer

With Photo(gCurrent)
.FormPhoto = New frmPhoto
.FormPhoto.Text = PhotoName
.FormPhoto.Tag = CStr(gCurrent) ' per associare il form alla foto
corrente
.FormPhoto.Show()
.PhotoName= PhotoName

If Origin = 0 Then ' must read from bmp file

Try
'Dim size As Size =
System.Drawing.Image.FromFile(PhotoName).Size
'.FormPhoto.picPhoto.Size = size
'.FormPhoto.picFoto.BackgroundImage =
System.Drawing.Image.FromFile(NomePhoto)

FileOpen(1, PhotoName, OpenMode.Binary)
'UPGRADE_WARNING: Get was upgraded to FileGet and has a
new behavior. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keywor d="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
FileGet(1, pixelOffset, 11)
'UPGRADE_WARNING: Get was upgraded to FileGet and has a
new behavior. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keywor d="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
FileGet(1, w, 19) 'Width
'UPGRADE_WARNING: Get was upgraded to FileGet and has a
new behavior. Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keywor d="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
FileGet(1, h, 23) 'Height
.w = w
.w3 = Multiple4(w * 3) 'width and height are read
correctly
.h = h
.StartPixel = 1 + pixelOffset


ReDim .header(pixelOffset - 1)
"'
FileGet(1, .header, 1)

ReDim .g(.w3, .h) 'transpose matrix


FileGet(1, .g(.w3 - 1, .h - 1), .StartPixel ) 'ERROR
HERE, matrix is filled with nothing,beginning read from StartPixel

FileClose(1)

End With
End Sub

"Andrew Morton" ha scritto nel messaggio
news:eoeu$o9FIHA.1184@TK2MSFTNGP04.phx.gbl...
> Marco Biagioni wrote:
>> FILEGET(1,PHOTO.MATRIX(PHOTO.WIDTH*3,PHOTO.HEIGHT),STARTPIXE L)
>
> Looking at the help for FileGet, shouldn't that be
>
> FileGet(1, PHOTO.MATRIX)
>
> ?
>
> Andrew
>

Re: Recover data from a bitmap and filling a matrix

am 29.10.2007 11:04:51 von Andrew Morton

Marco Biagioni wrote:
> in fileget function i must specify dimension of matrix otherwise it
> gives me an error...

What error? Do you have Option Strict On before your code?

The way it looks to me is that you are trying to put a single value into the
last element of the array. The docs specify that you pass an array, not the
last element of the array, if you want to read into an array.

For example,

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim a(,) As Byte = {{1, 2, 3}, {4, 5, 6}}
Dim handle As Integer = FreeFile()
FileOpen(handle, "C:\Temp\test.dat", OpenMode.Binary)
FilePut(handle, a)
FileClose(handle)
Dim b(1, 2) As Byte
handle = FreeFile()
FileOpen(handle, "C:\Temp\test.dat", OpenMode.Binary)
FileGet(handle, b)
FileClose(handle)
For i As Integer = 0 To 1
For j As Integer = 0 To 2
Debug.WriteLine(b(i, j))
Next
Next
End Sub

Outputs 1 2 3 4 5 6, and the file test.dat consists of the bytes 1 4 2 5 3 6
(in that order).

Andrew