Hi buddies:
I wanna write a program in Visual Basic which can read a BMP file and convert it into a Matrix... can anybody have any idea or experience about this.. Help me.. i will really appreciate it..
Thank you,
Farshid
Printable View
Hi buddies:
I wanna write a program in Visual Basic which can read a BMP file and convert it into a Matrix... can anybody have any idea or experience about this.. Help me.. i will really appreciate it..
Thank you,
Farshid
Like a matrix of RGB values?
(make sure to define GetPixel as in API Viewer...) :rolleyes:
(hehe, he called me a buddy :)) - thanks man, means the world to me *sob* :D
j/k :rolleyes:
Code:Dim hMat() as RGBT
Type RGBT
R as Single
G as Single
B as Single
End Type
Function GetR(ByVal pCol As Long) As Byte
GetR = pCol Mod 256
End Function
Function GetG(ByVal pCol As Long) As Byte
pCol = pCol \ 256
GetG = pCol Mod 256
End Function
Function GetB(ByVal pCol As Long) As Byte
pCol = (pCol \ 256) \ 256
GetB = pCol Mod 256
End Function
Sub ConvertToMatix(hDC as Long, W, H, hMatrix as RGBT)
dim TEMP as Long
Redim hMatrix(0 to W, 0 to H) as RGBT
Dim I, J
For I = 0 to W
For J = 0 to H
TEMP = GetPixel(hDC, I, J)
hMatrix(I, J).R = GetR(TEMP)
hMatrix(I, J).G = GetG(TEMP)
hMatrix(I, J).B = GetB(TEMP)
Next J
Next I
End Sub
'usage:
ConvertToMatrix Pic1.hDC, Pic1.ScaleWidth, Pic1.ScaleHeight, hMat
Hi budy Sastraxi:
Thank you very much for the help... I am a beginner in Visual Basic Programming for Graphics.. The assignment that i have been given is that i should develop a program or rather a function which should open a bmp file, read it and convert save the image data in a matrix. So this is all what i want. can u help me in this regard. I will be really thankful to you for it..
bye..
Farshid
hmmm.. i thought i posted what you wanted....
Ah well, please explain what you want to DO with this "matrix" after you have gathered the data (if not in the way i showed then what?)
if you cant explain it well here drop me an email:
[email protected]
And check out my soon-to-be website:
http://vbden.com-1.net/
-or-
http://vbden.tripod.com/
Hi budy...
Here is my problem.
I want to write a program which can can input bmp image (Black and white colors only) and then read the bmp file... save the image information in a matrix (array) and then store the matrix in a file...
this is the whole problem.. i am really working hard on it.. ur cooperation will be highly appreciated buddy...
thanks for everything...
farshid
Allright! Use the above code and the usage line to convert to an array. The above code will work for 24-bit colour bitmaps. Anyways, then, once you have the array:
Just make sure to dimension the array properly when loading the file. (OpenMatrix)Code:Function StoreMatrix(hMatrix as RGBT, FileName as String)
Dim bI, bJ, I, J
bI = Ubound(hMatrix, 1)
bJ = Ubound(hMatrix, 2)
Open FileName for Input as #1
For I = 0 to bI: For J = 0 to bJ
Put #1, CSTR(SplitColours(hMatrix(I, J))
Next I: Next J
Close #1
End Function
Function OpenMatrix(hMatrix as RGBT, FileName as String)
Dim bI, bJ, I, J
Dim tMatrix() as Long
bI = Ubound(hMatrix, 1)
bJ = Ubound(hMatrix, 2)
Redim tMatrix(0 to bI, 0 to bJ) as Long
Open FileName for Output as #1
For I = 0 to bI: For J = 0 to bJ
Get #1, tMatrix(I, J)
hMatrix(I, J).R = GetR(tMatrix(I, J)
hMatrix(I, J).G = GetG(tMatrix(I, J)
hMatrix(I, J).B = GetB(tMatrix(I, J)
Next I: Next J
Close #1
End Function
Function SplitColours(Ins as RGBT) as Long
SplitColours = RGB(Ins.R, Ins.G, Ins.B)
End Function
USAGE:
[code]
Dim hMat() as RGBT
ConvertToMatrix hMat, 20, 20, Picture1.hDC
StoreMatrix hMat, "C:\MyMat.XXX"
...
Dim myMat(20, 20) as RGBT
OpenMatrix myMat, "C:\MyMat.XXX"
Hi friend:
Thank you very much for your help. I will study the code and see whether i will be able to solve my problem with it or not.. but hopefully i will... if anything i will just mail u..
ok..thanks alot once again for ur cooperation...
farshid
I don't get it, the bmp file format contains this "matrix" already.
I think he wants to write it in plain text, so you could open it by just line inputting and saving that value in a pixel, until the end of file.
eh, i thought he refered to an 2darray with "matrix"
Well If you look at the code I posted then you will see that it is both.
By taking a quick look on the first part of the code (posted by Sastraxi), I understand you split the three components of the BMP image.
However, this is done using a handle to a picture control, isn't it ?? ( ConvertToMatrix Pic1.hDC, Pic1.ScaleWidth, Pic1.ScaleHeight, hMat )
Can this be done by reading the bitmap as a BINARY FILE directly ? Do you know the binary format of the BMP files ?
You could load it, make a compatible DC with the DIB data, and then call the DC in along with the array and the height and width in the DIB Type structure.
Please explain more on the BMP binary file format. (type, length of records, headers etc.) :confused: :confused: :confused:
Is that what you were trying to make me understand in your last reply ? :confused: :confused: :confused:
http://rookscape.com/vbgaming/tutAX.php
This tutorials shows you two things, first of all how to load the bitmap data into UDTs and an array, and secondly how to use that data directly to blit the bitmap to a DC...
You can easily pick out the first part though, which shows how the bitmap format is put together...