取JPG图像的实际尺寸(200分)

  • 主题发起人 主题发起人 recowen
  • 开始时间 开始时间
R

recowen

Unregistered / Unconfirmed
GUEST, unregistred user!
可否用
OpentextFIle("a.jpg")即读文件的方式取出JPG文件的图像实际尺寸??
不用控件。
涉及JPG文件格式问题
 
可以用变通的办法:
uses jpeg;
procedure TForm1.Button1Click(Sender: TObject);
var b:TBitmap; jpg:TJpegImage;
begin
jpg:=TJpegImage.Create;
jpg.LoadFromFile('c:/test.jpg');
b:=TBitmap.Create;
b.Assign(jpg);
Caption:=IntTostr(b.Width)+','+IntToStr(b.Height);
b.Free;
jpg.Free;
end;
 
我可真蠢,

var jpg:TJpegImage;
begin
jpg:=TJpegImage.Create;
jpg.LoadFromFile('c:/test.jpg');
Caption:=IntTostr(jpg.Width)+','+IntToStr(jpg.Height);
jpg.Free;
end;
 
的确,TJpegImage的Bitmap始终是空的。
 
说错了。其实TJpegImage的Bitmap不是空的;它是被申明为Protected;
可以通过继承来获得。
type
TJ=class(TJpegImage)
end;

procedure JPEG2BMP;
var FJ: TJ; FB: TBitmap;
begin
FJ:=TJ.Create;
FB:=TBitmap.Create;
try
FJ.LoadFromFile('C:/test.JPG');
FB.Assign(FJ.Bitmap);
FB.SaveToFile('C:/Test.bmp');
finally
FB.Free;
FJ.Free;
end;
end;
 
直接PARSEJPEG文件可以得到其宽度和高度,这都存在JPEG文件里.
但是其值是二进制的,而不是文本方式的,不可能用度文本的方式得
到.

JPEG文件头的格式较复杂,你可以上网找一下,资料还是比较多.
 
lhz大侠果然不凡,不要老是不来啊,经常过来帮助同志们……
 
这是VB的方法!

Public Function GetImageSize(sFileName As String) As ImageSize
On Error Resume Next 'you'll want to change this
Dim iFN As Integer
Dim bTemp(3) As Byte
Dim lFlen As Long
Dim lPos As Long
Dim bHmsb As Byte
Dim bHlsb As Byte
Dim bWmsb As Byte
Dim bWlsb As Byte
Dim bBuf(7) As Byte
Dim bDone As Byte
Dim iCount As Integer

lFlen = FileLen(sFileName)
iFN = FreeFile
Open sFileName For Binary As iFN
Get #iFN, 1, bTemp()

'PNG file
If bTemp(0) = &H89 And bTemp(1) = &H50 And bTemp(2) = &H4E _
And bTemp(3) = &H47 Then
Get #iFN, 19, bWmsb
Get #iFN, 20, bWlsb
Get #iFN, 23, bHmsb
Get #iFN, 24, bHlsb
GetImageSize.Width = CombineBytes(bWlsb, bWmsb)
GetImageSize.Height = CombineBytes(bHlsb, bHmsb)
End If

'GIF file
If bTemp(0) = &H47 And bTemp(1) = &H49 And bTemp(2) = &H46 _
And bTemp(3) = &H38 Then
Get #iFN, 7, bWlsb
Get #iFN, 8, bWmsb
Get #iFN, 9, bHlsb
Get #iFN, 10, bHmsb
GetImageSize.Width = CombineBytes(bWlsb, bWmsb)
GetImageSize.Height = CombineBytes(bHlsb, bHmsb)
End If


'JPEG file
If bTemp(0) = &HFF And bTemp(1) = &HD8 And bTemp(2) = &HFF Then
Debug.Print "JPEG"
lPos = 3
Do
Do
Get #iFN, lPos, bBuf(1)
Get #iFN, lPos + 1, bBuf(2)
lPos = lPos + 1
Loop Until (bBuf(1) = &amp;HFF And bBuf(2) <> &amp;HFF) Or lPos > lFlen

For iCount = 0 To 7
Get #iFN, lPos + iCount, bBuf(iCount)
Next iCount
If bBuf(0) >= &amp;HC0 And bBuf(0) <= &amp;HC3 Then
bHmsb = bBuf(4)
bHlsb = bBuf(5)
bWmsb = bBuf(6)
bWlsb = bBuf(7)
bDone = 1
Else
lPos = lPos + (CombineBytes(bBuf(2), bBuf(1))) + 1
End If
Loop While lPos < lFlen And bDone = 0
GetImageSize.Width = CombineBytes(bWlsb, bWmsb)
GetImageSize.Height = CombineBytes(bHlsb, bHmsb)

End If
Close iFN

End Function
Private Function CombineBytes(lsb As Byte, msb As Byte) As Long
CombineBytes = CLng(lsb + (msb * 256))
End Function


 
可以将VB改DElPHI方法
www.csdn.net中有详尽的程序
 
多人接受答案了。
 
后退
顶部