|
楼主 |
发表于 2018-8-20 17:39
|
显示全部楼层
下面的代码 适用范围更大,如果 put #1 则输出为 图片 1.jpg。
Sub 按钮1_Click()
Dim fn, f
Dim arr() As Byte, H, i
Range("a:a").Clear
fn = Application.GetOpenFilename("图像文件,*.bmp", , "请选文件", , MultiSelect:=True)
If Not IsArray(fn) Then Exit Sub
For Each f In fn
Open f For Binary As #1
H = LOF(1)
ReDim arr(1 To H)
Get #1, , arr
Close #1
For i = 1 To 54
Cells(i, 1) = arr(i)
Next
'Open ThisWorkbook.Path & "\1.jpg" For Binary As #1
'Put #1, , arr
'Close #1
For i = 0 To 3
PixelCol = PixelCol + arr(i + 19) * 256 ^ i
Next
For i = 0 To 3
PixelRow = PixelRow + arr(i + 23) * 256 ^ i
Next
'获取图片的像素高和宽。宽度不是4的倍数时需补零
If PixelCol Mod 4 <> 0 Then Zeroize = PixelCol Mod 4
Worksheets("sheet1").Activate
Cells.Clear
With Worksheets("sheet1")
For i = PixelRow To 1 Step -1
CellRow = CellRow + 1
CellCol = 0
For j = 1 To PixelCol * 3 Step 3
CellCol = CellCol + 1
lngPos = arr(11) + j + (i - 1) * (PixelCol * 3 + Zeroize)
.Cells(CellRow, CellCol).Interior.Color = RGB(arr(lngPos + 2), arr(lngPos + 1), arr(lngPos))
Next
Next
End With
Next
End Sub
|
|