一个取图片尺寸的类支持jpg,gif,png

2016-02-19 22:35 14 1 收藏

下面是个简单易学的一个取图片尺寸的类支持jpg,gif,png教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - Web开发 】

'I have released this source code into the public domain. You may use it
'with no strings attached.
'Just call GetImageSize with a string containing the filename, and
'it will return a user defined type 'ImageSize' (see below)
'Return values of 0 indicate an error of some sort. The error handling
'in this module is limited. There is *NO* error handling on the test
'form. This routine is limited to X or Y sizes of 32767 pixels, but that
'should not be a problem.

'Check back athttp://www.qtm.net/~davidc
'I may add support for more file types.

'supported in this version:
'JPEG
'GIF
'PNG

'This routine does not require any royalty fees for Unisys as it
'does nothing with the compressed part of GIF files. It simply reads
'4 bytes to determine image size.

Option Explicit
Public WImg As Long
Public HImg As Long
Public Type ImageSize
Width As Long
Height As Long
End Type

Public Sub GetImageSize(sFileName As String)
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)
WImg = CombineBytes(bWlsb, bWmsb)
HImg = CombineBytes(bHlsb, bHmsb)
End If

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

'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)
WImg = CombineBytes(bWlsb, bWmsb)
HImg = 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) = &HFF And bBuf(2) &HFF) Or lPos lFlen

For iCount = 0 To 7
Get #iFN, lPos + iCount, bBuf(iCount)
Next iCount
If bBuf(0) = &HC0 And bBuf(0) = &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)
WImg = CombineBytes(bWlsb, bWmsb)
HImg = CombineBytes(bHlsb, bHmsb)
End If
Close iFN

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)

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

来源:http://www.tulaoshi.com/n/20160219/1628556.html

延伸阅读
标签: Web开发
问题是一个网友提出来的,想要达到的效果是:如果是图片宽高比大于150/240,以宽为主,宽=150,高度自动,否则,以高为主,高=240,宽度自动, !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" html head meta http-equiv="Content-Type" content="te...
微信公众平台图片尺寸是多少?   微信公众平台的头像图片官方尺寸是360*200,小编建议最好的尺寸是200*200,毕竟手机可以随着图片的大小,200*200是最清晰的微信头像.方法如下所示: 1、登录您的微信公众平台,点击素材管理,选择多图文消息 2、一般官方建议的是360*200,但在手机微信上显示的图片会被标题挡住一部分,所以...
标签: word 压缩图片
Word 2007技巧:自动压缩图片尺寸 尽管用户可以在Word 2007文档中压缩所有图片或选中图片的尺寸,以减小Word文件的大小,但每次都执行压缩操作未免太繁琐。用户可以设置Word 2007图片压缩选项,实现在保存Word 2007文档时自动压缩图片尺寸的目的,操作步骤如下所述: 第1步,打开Word 2007文档窗口,选中任意图片。在打开的Word 2007...
标签: windows 操作系统
用数码相机拍摄的照片,通过数据线传到电脑上后尺寸比较大。为了方便浏览,我们通常都在Photoshop等专业图像处理软件中一张张来调整图片的大小。其实还有更多更方便简捷的方法来完成。 一、在ACDSee中调整 ACDSee是目前最流行的数字图像处理软件,批量调整图片尺寸对它来说简直是“小菜一碟”。 1.打开ACDSee 7.0(至...
Linux系统如何批量压缩图片尺寸大小 现在的数码相机拍摄出来的照片像素都很高,大部份都会超过此限制,很多时候我们都要对图片进行批量压缩,Linux系统下如何批量压缩图片尺寸大小呢? 一个比较好用命令行下可以使用的 强大的免费图片工具 ImageMagick Centos系统的安装使用方法: yum install ImageMagick 再输入y确...

经验教程

108

收藏

78
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部