如何获得一个图片文件的宽度和高度,……

litaoa 2002-04-09 02:47:52
我用image控件image=loadpicture(****)加载图片,我用msgbox image.Height 和
msgbox image.Width得到的图片大小和我用其它工具得到不一样,我该怎么才能得到它的大小,谢谢了,
...全文
139 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
litaoa 2002-04-10
  • 打赏
  • 举报
回复
谢谢大家了我还是用pic控件的方法吧…对我来说比较简单…
uguess 2002-04-09
  • 打赏
  • 举报
回复

试试这个程序!

'The Bitmap Info project demonstrates one means of obtaining from a bitmap file on the disk its image information without loading the actual bitmap.
'
'Within a bitmap file are 2 header groupings of information describing the bitmap image in that file. This data is contained in the file's BITMAPFILEHEADER and BITMAPINFOHEADER structures. By using the VB Type to recreate these structures, the details of the bitmap contained in the file can be obtained with a simple binary read of this data.
'(Note: due to the formatting of the code to allow pasting into VB, the strings used here will necessitate horizontal scrolling of the browser window on displays under 1024x768 full screen.)
'
'
' Form Code
'
'To a form add the controls as indicated in the illustration. To see a template of this form with control names substituted for the control captions, click here to open the image in a new window.
'
'The names of the description labels is unimportant, but the data returned from the routine below posts the information to the lblInfo() label array. Also on the form is an image control (Image1), with its Stretch property set to True, two command buttons (cmdSelect and cmdEnd), and another label to contain the selected filename and path (lblFileName). Finally, the routine needs a common dialog control (CMDialog1) added to the form.
'
'Paste the following code into the general declarations section of the form:

'--------------------------------------------------------------------------------

Option Explicit

Private Const CANCELERR = 32755
Private Const BI_RGB = 0&
Private Const BI_RLE8 = 1&
Private Const BI_RLE4 = 2&
Private Const BI_BITFIELDS = 3&

Private Type BITMAPINFOHEADER '40 bytes
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 Type

Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type


Private Sub Form_Load()

'initialize the form controls
lblFileName = "Select a bitmap or RLE file to detail..."
lblInfo(0) = ""
lblInfo(1) = ""
lblInfo(2) = ""
lblInfo(3) = ""
lblInfo(4) = ""
lblInfo(5) = ""
lblInfo(6) = ""
lblInfo(7) = ""

'position the form
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

End Sub


Private Sub cmdEnd_Click()

Unload Me

End Sub


Private Sub cmdSelect_Click()

'create some working variables
Dim ff As Integer
Dim tmp As String

'create the variables to hold the bitmap info
Dim FileHeader As BITMAPFILEHEADER
Dim InfoHeader As BITMAPINFOHEADER

On Error GoTo cmdSelect_FileErrorHandler

'show the common dialog
CMDialog1.CancelError = True
CMDialog1.ShowOpen

'display a rendition of the loaded bitmap
Image1 = LoadPicture((CMDialog1.filename))
Image1.ZOrder 1

'read the file header info
ff = FreeFile
Open CMDialog1.filename For Binary Access Read As #ff
Get #ff, , FileHeader
Get #ff, , InfoHeader
Close #ff

'display the file info
lblFileName = CMDialog1.filename

lblInfo(0) = InfoHeader.biWidth & " pixels"
lblInfo(1) = InfoHeader.biHeight & " pixels"

'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biSizeImage
Case 0: tmp$ = "BI_RGB bitmap; size variable not filled in."
Case Else: tmp$ = Format$(InfoHeader.biSizeImage, "#,###,###") & " bytes"
End Select
lblInfo(2) = tmp$

lblInfo(3) = InfoHeader.biPlanes
lblInfo(4) = InfoHeader.biBitCount & " (" & 2 ^ InfoHeader.biBitCount & " colours)"

'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biCompression
Case BI_RGB: tmp$ = "Uncompressed bitmap."
Case BI_RLE8: tmp$ = "Run-length encoded (RLE) format for bitmaps with 8 bits per pixel."
Case BI_RLE4: tmp$ = "Run-length encoded (RLE) format for bitmaps with 4 bits per pixel."
Case BI_BITFIELDS: tmp$ = "Uncompressed 16- or 32-bit-per-pixel format."
End Select
lblInfo(5) = tmp$

'select the appropriate string based on the value of biClrUsed
Select Case InfoHeader.biClrUsed
Case 0:
tmp$ = "Bitmap uses the maximum number of colours corresponding to the"
tmp$ = tmp$ & " bits-per-pixel for the compression mode."

Case Is <> 0 And InfoHeader.biBitCount = 16:
tmp$ = "The size of the colour table used to optimize performance"
tmp$ = tmp$ & "of Windows colour palettes is " & Str$(InfoHeader.biClrUsed)
End Select
lblInfo(6) = tmp$

'select the appropriate string based on the value of biClrImportant
Select Case InfoHeader.biClrImportant
Case 0:
tmp$ = "All " & 2 ^ InfoHeader.biBitCount & " colour"
tmp$ = tmp$ & " indices are considered important for displaying this bitmap."
Case Is <> 0
tmp$ = "The number of colours that are considered important for displaying"
tmp$ = tmp$ & " this bitmap are " & Str$(InfoHeader.biClrImportant)
End Select
lblInfo(7) = tmp$

Exit Sub

'handle file errors or the user choosing cancel
cmdSelect_FileErrorHandler:

If Err <> CANCELERR Then MsgBox Error$(Err), 48, "Image Info"
lblFileName = "No file was selected."

End Sub
寒雪叔叔 2002-04-09
  • 打赏
  • 举报
回复
如果知道这个文件的格式就好办了
比如为bmp格式,只要读出这个文件的文件头就行了,
先读一个BITMAPFILEHEADER,再读一个别BITMAPINFOHEADER
这个图形的宽高就在这个BITMAPINFOHEADER里面了。其它的格式方法差不多
stormday 2002-04-09
  • 打赏
  • 举报
回复
用Picture控件,AutoSize=True.
访问它的Height和Width属性,获得高与宽。
为了美观,可以将它的Visible属性设为不可见。
playyuer 2002-04-09
  • 打赏
  • 举报
回复
MsgBox "Size: " & form1.ScaleX(p.Width,8,3) & " × " & form1.ScaleY(p.Height,8,3)

如何正确得出 Picture 的宽、高的像素数????
http://www.csdn.net/Expert/TopicView1.asp?id=258476
gump2000 2002-04-09
  • 打赏
  • 举报
回复
用pictureBox控件
AutoSize=True
就可以了
另外
scalemode=3(pixel)
msgbox pic1.scalewidth & ":" & pic1.scaleheight
就识了

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧