一个按钮,一个textbox Private Type mybmpinfo bfType As String * 2 bfSize As Long bfReserved1 As Integer bfReserved2 As Integer bfOffBits As Long biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End TypePrivate Sub Command1_Click() On Error Resume Next Me.CommonDialog1.Filter = "bmp files(*.bmp)|*.bmp|JPG files(*.jpg)|*.jpg" Me.CommonDialog1.ShowOpen Dim mfile As String mfile = Me.CommonDialog1.FileName Picture1.Picture = LoadPicture(mfile) SavePicture Picture1.Picture, App.Path + "\mytemp.bmp" Dim mbmpinfo As mybmpinfo Open App.Path + "\mytemp.bmp" For Binary As #1 Get #1, , mbmpinfo Close #1 Text1.Text = "颜色数:2^" + CStr(mbmpinfo.biBitCount) Kill App.Path + "\mytemp.bmp" End Sub
这个话题很高级,一般用八叉树算法来计算.octree(英文名),去搜索下吧。
To rainstormmaster(rainstormmaster):你这样算太不科学了吧,注意题目"怎样快速获得一幅图像所用的颜色的总数?",注意"所用"这两个字.
Private Type mybmpinfo
bfType As String * 2
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End TypePrivate Sub Command1_Click() On Error Resume Next
Me.CommonDialog1.Filter = "bmp files(*.bmp)|*.bmp|JPG files(*.jpg)|*.jpg"
Me.CommonDialog1.ShowOpen
Dim mfile As String
mfile = Me.CommonDialog1.FileName
Picture1.Picture = LoadPicture(mfile)
SavePicture Picture1.Picture, App.Path + "\mytemp.bmp"
Dim mbmpinfo As mybmpinfo
Open App.Path + "\mytemp.bmp" For Binary As #1
Get #1, , mbmpinfo
Close #1
Text1.Text = "颜色数:2^" + CStr(mbmpinfo.biBitCount)
Kill App.Path + "\mytemp.bmp"
End Sub
http://www.gameres.com/Articles/articles.asp?style=a