我先将图像中某点(比如说0,0点)的灰度读取出来:
rgb1 = GetPixel(picbox.hdc, 0, 0)
bblue1 = Blue(rgb1)
bred1 = Red(rgb1)
bgreen1 = Green(rgb1)
grayvalue = 0.3 * bred1 + 0.59 * bgreen1 + 0.11 * bblue1'灰度然后我希望使该点(0,0点)的灰度值提升2,于是:
SetPixelV hdc1, 0, 0, RGB(grayvalue+2,grayvalue+2,grayvalue+2)
SavePicture , App.Path & "\test.bmp"但是,当我打开重新打开文件,发现,灰度并没有改变。
picbox.picture=loadpicture app.path & "\test.bmp"
rgb1 = GetPixel(picbox.hdc, 0, 0)
bblue1 = Blue(rgb1)
bred1 = Red(rgb1)
bgreen1 = Green(rgb1)
grayvalue = 0.3 * bred1 + 0.59 * bgreen1 + 0.11 * bblue1'灰度
此时的grayvalue还是等于以前的那个。
为什么啊????????
rgb1 = GetPixel(picbox.hdc, 0, 0)
bblue1 = Blue(rgb1)
bred1 = Red(rgb1)
bgreen1 = Green(rgb1)
grayvalue = 0.3 * bred1 + 0.59 * bgreen1 + 0.11 * bblue1'灰度然后我希望使该点(0,0点)的灰度值提升2,于是:
SetPixelV hdc1, 0, 0, RGB(grayvalue+2,grayvalue+2,grayvalue+2)
SavePicture , App.Path & "\test.bmp"但是,当我打开重新打开文件,发现,灰度并没有改变。
picbox.picture=loadpicture app.path & "\test.bmp"
rgb1 = GetPixel(picbox.hdc, 0, 0)
bblue1 = Blue(rgb1)
bred1 = Red(rgb1)
bgreen1 = Green(rgb1)
grayvalue = 0.3 * bred1 + 0.59 * bgreen1 + 0.11 * bblue1'灰度
此时的grayvalue还是等于以前的那个。
为什么啊????????
....
picture1.autoredraw=true
set picture1.picture=picture1.image
SavePicture , App.Path & "\test.bmp"
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End TypePublic Type tpBitMapInfoHeader
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 TypePublic Type tpBitMapHeader
bhFileHeader As tpBitMapFileHeader
bhInfoHeader As tpBitMapInfoHeader
End Type先从tpBitMapInfoHeader里得到Width和Height,然后定义一个RGB数组。RGB类型是这样定义的:Type tpRGB24
Blue As Byte
Green As Byte
Red As Byte
End Type然后定义数组Dim sysPixs(Width,Height) As tpRGB24接着你从文件里读取数组。然后可以这样。L=2
sysPixs(X,Y).Blue=sysPixs(X,Y).Green+0.11*L
sysPixs(X,Y).Green=sysPixs(X,Y).Green+0.59*L
sysPixs(X,Y).Red=sysPixs(X,Y).Green+0.3*L接着写到文件里就可以了。
存储地址=坐标地址*色深+偏移量在BMP文件当中,坐标地址是反的。也就是最后一个像素减去当前像素才是。如果你听不懂,稍后我会写出一个函数提供给你。
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "Form1"
ClientHeight = 5475
ClientLeft = 60
ClientTop = 345
ClientWidth = 6525
LinkTopic = "Form1"
ScaleHeight = 365
ScaleMode = 3 'Pixel
ScaleWidth = 435
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
Height = 270
Left = 120
TabIndex = 0
Text = "Text1"
Top = 120
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
Dim tApplic As tpBMP_Applic
Dim tBitMapInfo As tpBitMapInfo
tApplic = BMP_Applic_GetByFile("Test.bmp")
tBitMapInfo.bmiHeader = tApplic.baHeader.bhInfoHeader
Dim tX As Long
Dim tY As Long
Dim tC As Long
For tX = 0 To 800
For tY = 0 To 200
'tY = Cos(tX) * 100 + 50
Select Case tY
Case 0 To tApplic.baHeader.bhInfoHeader.biHeight
tC = tApplic.baPixels(tX + tY * 800).rgbBlue + 50 * 0.11
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * 800).rgbBlue = tC
tC = tApplic.baPixels(tX + tY * 800).rgbGreen + 50 * 0.5
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * 800).rgbGreen = tC
tC = tApplic.baPixels(tX + tY * 800).rgbRed + 50 * 0.39
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * 800).rgbRed = tC
End Select
Next tY
Next
'Form1.Show
Form1.AutoRedraw = True
Text1.Text = StretchDIBits(Form1.hDC, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, 0, 0, tBitMapInfo.bmiHeader.biWidth, tBitMapInfo.bmiHeader.biHeight, tApplic.baPixels(0), tBitMapInfo, 0, SRCCOPY)
BMP_Applic_PutToFile "TestOut1.bmp", tApplic
'Form1.AutoRedraw = False
'Text1.Text = StretchDIBits(Form1.hDC, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, 0, 0, tBitMapInfo.bmiHeader.biWidth, tBitMapInfo.bmiHeader.biHeight, tApplic.baPixels(0), tBitMapInfo, 0, SRCCOPY)
'Form1.Cls
End Sub模块文件BMP.basAttribute VB_Name = "Module1"
Public Type tpBitMapFileHeader
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End TypePublic Type tpBitMapInfoHeader
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 TypePublic Type tpRGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End TypePublic Type tpPixelRGB24
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
End TypePublic Type tpBitMapInfo
bmiHeader As tpBitMapInfoHeader
bmiColors As tpRGBQuad
End TypePublic Type tpBMP_FileHeader
bhFileHeader As tpBitMapFileHeader
bhInfoHeader As tpBitMapInfoHeader
End TypePublic Type tpBMP_Applic
baHeader As tpBMP_FileHeader
baPixels() As tpPixelRGB24
End TypePublic Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal wSrcWidth As Long, ByVal wSrcHeight As Long, lpBits As Any, lpBitsInfo As tpBitMapInfo, ByVal wUsage As Long, ByVal dwRop As Long) As LongPublic Const DIB_PAL_COLORS = 1Public Const DIB_RGB_COLORS = 0Public Const SRCCOPY = &HCC0020Function BMP_Applic_PutToFile(ByVal pFileName As String, ByRef pApplicData As tpBMP_Applic)
Dim tFileNumber As Integer
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber Put #tFileNumber, 1, pApplicData.baHeader
Put #tFileNumber, Len(pApplicData.baHeader) + 1, pApplicData.baPixels()
Close #tFileNumber
End FunctionFunction BMP_Applic_GetByFile(ByVal pFileName As String) As tpBMP_Applic
Dim tOutAny As tpBMP_Applic
Dim tFileNumber As Integer
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber
Dim tWidth As Long
Dim tHeight As Long
Dim tPixelsCount As Long Get #tFileNumber, 1, tOutAny.baHeader
tWidth = tOutAny.baHeader.bhInfoHeader.biWidth
tHeight = tOutAny.baHeader.bhInfoHeader.biHeight
tPixelsCount = (tWidth * tHeight) - 1
ReDim tOutAny.baPixels(tPixelsCount)
Get #tFileNumber, Len(tOutAny.baHeader) + 1, tOutAny.baPixels()
Close #tFileNumber
BMP_Applic_GetByFile = tOutAny
End Function
出错,说下标越界。调试程序发现,Get #tFileNumber, 1, tOutAny.baHeader
tWidth = tOutAny.baHeader.bhInfoHeader.biWidth
tHeight = tOutAny.baHeader.bhInfoHeader.biHeight
tPixelsCount = (tWidth * tHeight) - 1
都是空的,也就是根本没有读取到文件头信息啊。
应该是这个语句:Get #tFileNumber, 1, tOutAny.baHeader有问题。期待解答,谢谢
OPEN在不同文件时可用INPUT,WRITE,PUT三种语句写入。
Private Sub Form_Load()
Dim tApplic As tpBMP_Applic
Dim tBitMapInfo As tpBitMapInfo
tApplic = BMP_Applic_GetByFile("Test7.bmp")
tBitMapInfo.bmiHeader = tApplic.baHeader.bhInfoHeader
Dim tX As Long
Dim tY As Long
Dim tC As Long
For tX = 0 To tApplic.baHeader.bhInfoHeader.biWidth
For tY = 0 To tApplic.baHeader.bhInfoHeader.biHeight \ 2
'tY = Cos(tX) * 100 + 50
Select Case tY
Case 0 To tApplic.baHeader.bhInfoHeader.biHeight
tC = tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbBlue + 50 * 0.11
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbBlue = tC
tC = tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbGreen + 50 * 0.5
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbGreen = tC
tC = tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbRed + 50 * 0.39
If tC > 255 Then tC = 255
tApplic.baPixels(tX + tY * tApplic.baHeader.bhInfoHeader.biWidth).rgbRed = tC
End Select
Next tY
Next
'Form1.Show
Form1.AutoRedraw = True
Text1.Text = StretchDIBits(Form1.hDC, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, 0, 0, tBitMapInfo.bmiHeader.biWidth, tBitMapInfo.bmiHeader.biHeight, tApplic.baPixels(0), tBitMapInfo, 0, SRCCOPY)
BMP_Applic_PutToFile "TestOut1.bmp", tApplic
'Form1.AutoRedraw = False
'Text1.Text = StretchDIBits(Form1.hDC, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, 0, 0, tBitMapInfo.bmiHeader.biWidth, tBitMapInfo.bmiHeader.biHeight, tApplic.baPixels(0), tBitMapInfo, 0, SRCCOPY)
'Form1.Cls
End Sub
Dim tApplic As tpBMP_Applic
Dim tBitMapInfo As tpBitMapInfo
tBitMapInfo.bmiHeader = priBMP_ApplicData.baHeader.bhInfoHeader
Dim tX As Long
Dim tY As Long
Dim tC As Long
Dim tIndex As Long
Dim tIndexBase As Long
Dim tWidth As Long
Dim tHeight As Long
tWidth = priBMP_ApplicData.baHeader.bhInfoHeader.biWidth
tHeight = priBMP_ApplicData.baHeader.bhInfoHeader.biHeight
For tY = 0 To tHeight - 1
tIndexBase = tY * tWidth
For tX = 0 To tWidth - 1
tIndex = tX + tIndexBase
priBMP_ApplicData.baPixels(tIndex) = PixelAddBrightnes(priBMP_ApplicData.baPixels(tIndex), 50)
Next
Next
Text1.Text = BMP_Applic_ShowToForm(Form1, priBMP_ApplicData)
BMP_Applic_PutToFile "TestOut1.bmp", priBMP_ApplicData
End SubPrivate Sub Form_Load()
priBMP_ApplicData = BMP_Applic_GetByFile("Test.bmp")
End SubPrivate Sub Form_Resize()
Text1.Text = BMP_Applic_ShowToForm(Form1, priBMP_ApplicData)
End Sub'BMP.bas模块内容Attribute VB_Name = "Module1"
Public Type tpBitMapFileHeader
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End TypePublic Type tpBitMapInfoHeader
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 TypePublic Type tpRGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End TypePublic Type tpPixelRGB24
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
End TypePublic Type tpBitMapInfo
bmiHeader As tpBitMapInfoHeader
bmiColors As tpRGBQuad
End TypePublic Type tpBMP_FileHeader
bhFileHeader As tpBitMapFileHeader
bhInfoHeader As tpBitMapInfoHeader
End TypePublic Type tpBMP_Applic
baHeader As tpBMP_FileHeader
baPixels() As tpPixelRGB24
End TypePublic Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal wSrcWidth As Long, ByVal wSrcHeight As Long, lpBits As Any, lpBitsInfo As tpBitMapInfo, ByVal wUsage As Long, ByVal dwRop As Long) As LongPublic Const DIB_PAL_COLORS = 1Public Const DIB_RGB_COLORS = 0Public Const SRCCOPY = &HCC0020Function BMP_Applic_ShowToForm(ByRef pForm As Form, ByRef pApplicData As tpBMP_Applic) As Long
'将一个BMP_Applic显示在一个Form里。
Dim tOutLng As Long
Dim tBitMapInfo As tpBitMapInfo
Dim tPixels() As tpPixelRGB24
Dim tDesWidth As Long
Dim tDesHeight As Long
Dim tSurWidth As Long
Dim tSurHeight As Long
tBitMapInfo.bmiHeader = pApplicData.baHeader.bhInfoHeader
tPixels() = pApplicData.baPixels()
tDesWidth = pForm.ScaleWidth
tDesHeight = pForm.ScaleHeight
tSurWidth = tBitMapInfo.bmiHeader.biWidth
tSurHeight = tBitMapInfo.bmiHeader.biHeight
pForm.AutoRedraw = True
tOutLng = StretchDIBits(pForm.hDC, 0, 0, tDesWidth, tDesHeight, 0, 0, tSurWidth, tSurHeight, tPixels(0), tBitMapInfo, 0, SRCCOPY)
pForm.AutoRedraw = False
pForm.Cls
BMP_Applic_ShowToForm = tOutLng
End FunctionFunction BMP_Applic_PutToFile(ByVal pFileName As String, ByRef pApplicData As tpBMP_Applic)
'将一个BMP_Applic储存成BMP文件
Dim tFileNumber As Integer
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber Put #tFileNumber, 1, pApplicData.baHeader
Put #tFileNumber, Len(pApplicData.baHeader) + 1, pApplicData.baPixels()
Close #tFileNumber
End FunctionFunction BMP_Applic_GetByFile(ByVal pFileName As String) As tpBMP_Applic
'从文件读取一个BMP_Applic
Dim tOutAny As tpBMP_Applic
Dim tFileNumber As Integer
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber
Dim tWidth As Long
Dim tHeight As Long
Dim tPixelsCount As Long Get #tFileNumber, 1, tOutAny.baHeader
tWidth = tOutAny.baHeader.bhInfoHeader.biWidth
tHeight = tOutAny.baHeader.bhInfoHeader.biHeight
tPixelsCount = (tWidth * tHeight) - 1
ReDim tOutAny.baPixels(tPixelsCount)
Get #tFileNumber, Len(tOutAny.baHeader) + 1, tOutAny.baPixels()
Close #tFileNumber
BMP_Applic_GetByFile = tOutAny
End FunctionFunction PixelAddBrightnes(ByRef pPixel As tpPixelRGB24, ByVal pBrightnes As Byte) As tpPixelRGB24
'将一个Pixel表示的像素增加(或减少)亮度。
Dim tOutPixel As tpPixelRGB24
Dim tR As Long, tG As Long, tB As Long
tOutPixel.rgbRed = DataRulesLockSeg_Long(CLng(pPixel.rgbRed) + pBrightnes, 0, 255)
tOutPixel.rgbGreen = DataRulesLockSeg_Long(CLng(pPixel.rgbGreen) + pBrightnes, 0, 255)
tOutPixel.rgbBlue = DataRulesLockSeg_Long(CLng(pPixel.rgbBlue) + pBrightnes, 0, 255)
PixelAddBrightnes = tOutPixel
End FunctionFunction DataRulesLockSeg_Long(ByVal pValue As Long, ByVal pMin As Long, ByVal pMax As Long) As Long
'保证一个值Value在Min和Max之间。
If pValue > pMax Then
DataRulesLockSeg_Long = pMax
ElseIf pValue < pMin Then
DataRulesLockSeg_Long = pMin
Else
DataRulesLockSeg_Long = pValue
End If
End Function
Begin VB.Form Form1
AutoRedraw = -1 'True
Caption = "Form1"
ClientHeight = 5475
ClientLeft = 60
ClientTop = 345
ClientWidth = 6525
LinkTopic = "Form1"
ScaleHeight = 365
ScaleMode = 3 'Pixel
ScaleWidth = 435
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 255
Left = 5640
TabIndex = 1
Top = 120
Width = 855
End
Begin VB.TextBox Text1
Height = 270
Left = 120
TabIndex = 0
Text = "Text1"
Top = 120
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private priBMP_FileName As String
Private priBMP_ApplicData As tpBMP_ApplicPrivate Sub Command1_Click()
Dim tApplic As tpBMP_Applic
Dim tBitMapInfo As tpBitMapInfo
tBitMapInfo.bmiHeader = priBMP_ApplicData.baHeader.bhInfoHeader
Dim tX As Long
Dim tY As Long
Dim tC As Long
Dim tIndex As Long
Dim tIndexBase As Long
Dim tWidth As Long
Dim tHeight As Long
tWidth = priBMP_ApplicData.baHeader.bhInfoHeader.biWidth
tHeight = priBMP_ApplicData.baHeader.bhInfoHeader.biHeight
For tY = 0 To tHeight - 1
tIndexBase = tY * tWidth
For tX = 0 To tWidth - 1
tIndex = tX + tIndexBase
priBMP_ApplicData.baPixels(tIndex) = PixelAddBrightnes(priBMP_ApplicData.baPixels(tIndex), 50)
Next
Next
Text1.Text = BMP_Applic_ShowToForm(Form1, priBMP_ApplicData)
BMP_Applic_PutToFile "TestOut1.bmp", priBMP_ApplicData
End SubPrivate Sub Form_Load()
priBMP_ApplicData = BMP_Applic_GetByFile("Test.bmp")
End SubPrivate Sub Form_Resize()
Text1.Text = BMP_Applic_ShowToForm(Form1, priBMP_ApplicData)
End Sub