[Resolved] How to read Dimension of .bmp file ??
Hey guys
I am working on a project using visual basic 2005 which involves finding some data and adding it onto an existing excel file. the data in question are the dimensions (in pixels) of a folder full of .bmp images which is accepted from the user. i have to read the filename and the dimension of the images and then add the dimension to the excel file which already has a list of the filenames... so i need to add the dimensions alongside the filename in the excel file...
i am new to VB programming and dun rilli have any idea how to go about this problem.. please advise
cheers
Abhilash
Re: How to read Dimension of .bmp file ??
Moved from Application Deployment forum.
Re: How to read Dimension of .bmp file ??
Hey guys !!
can anyone help me out with this problem..?? i got a code
Option Explicit On
Private Structure BITMAPFILEHEADER
Dim bfType As Byte
Dim bfSize As Long
Dim bfReserved1 As Integer
Dim bfReserved2 As Integer
Dim bfOffBits As Long
End Structure
Private Structure BITMAPINFOHEADER
Dim biSize As Long
Dim biWidth As Long
Dim biHeight As Long
Dim biPlanes As Integer
Dim biBitCount As Integer
Dim biCompression As Long
Dim biSizeImage As Long
Dim biXPelsPerMeter As Long
Dim biYPelsPerMeter As Long
Dim biClrUsed As Long
Dim biClrImportant As Long
End Structure
Private Sub GetBitmapSize(ByVal Bitmap As String, ByRef lWidth As Long, ByRef lHeight As Long)
Dim hFile As Integer
Dim bfh As BITMAPFILEHEADER
Dim bih As BITMAPINFOHEADER
.hFile = FreeFile()
Open Bitmap For Binary Access Read Shared As #hFile
Get #hFile, , bfh
Get #hFile, , bih
Close #hFile
.lWidth = bih.biWidth
.lHeight = bih.biHeight
End Sub
Private Sub Command1_Click()
Dim lngWidth As Long
Dim lngHeight As Long
Call (GetBitmapSize("C:\00C0.bmp", lngWidth, lngHeight))()
MsgBox("Width: " & lngWidth & vbCrLf & "Height: " & lngHeight)
End Sub
This code has a few errors..
'Private Structure BITMAPFILEHEADER'
'Private Structure BITMAPINFOHEADER'
these two statements give the error
"Types declared 'Private' must be inside another type."
and
'Private Sub GetBitmapSize(ByVal Bitmap As String, ByRef lWidth As Long, ByRef lHeight As Long)'
'Private Sub Command1_Click()'
these two statements give error
"Statement is not valid in a namespace."
please advise
cheers
Abhilash
Re: How to read Dimension of .bmp file ??
The purpose of the above code is to accept a folder from the user which contains bitmap (.bmp) image files and read/calculate the dimensions (in pixels) of each image and then relate them to the filename of the image..
these dimensions then have to be inserted into a template excel file which already has the filenames and the code should write the dimensions alongside the respective filename in the excel sheet.
cheers
Abhilash
Re: How to read Dimension of .bmp file ??
Re: How to read Dimension of .bmp file ??
OK, I thought you were using VB6, because you posted in the classic VB forum. You mentioned you were using VB 2005, but I guess I missed that part.
There is an easy way of doing this in .NET, but this version loads the complete bitmap into memory, so the memory footprint is a lot bigger.
This is the GetBitmapSize1 function below.
I tried to translate the VB6 code to VB 2005, but I didn't find an easy way to load a structure from a file in .NET. I have changed the code a little bit to read integers. There is some reading at the start to get on the correct position in the file.
This is the GetBitmapSize2 function below.
Code:
Imports System.io
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As Size
s = GetBitmapSize1("c:\windows\zapotec.bmp")
MessageBox.Show("Width: " & s.Width.ToString & vbCrLf & "Height: " & s.Height.ToString)
s = GetBitmapSize2("c:\windows\zapotec.bmp")
MessageBox.Show("Width: " & s.Width.ToString & vbCrLf & "Height: " & s.Height.ToString)
End Sub
Private Function GetBitmapSize1(ByVal Path As String) As Size
If File.Exists(Path) Then
Dim b As New Bitmap(Path)
Dim s As New Size(b.Width, b.Height)
b.Dispose()
Return s
Else
Throw New FileNotFoundException()
End If
End Function
Private Function GetBitmapSize2(ByVal Path As String) As Size
If File.Exists(Path) Then
Dim s As Size
Dim br As New BinaryReader(File.Open(Path, FileMode.Open, FileAccess.Read))
Dim Buffer() As Byte
Dim Dummy As Integer
ReDim Buffer(13)
br.Read(Buffer, 0, 14) ' read the bitmapheader into the buffer, we don't use it
Dummy = br.ReadInt32 ' read the biSize member of the BITMAPINFOHEADER, we don't use it
s.Width = br.ReadInt32 ' read the biWidth member of the BITMAPINFOHEADER
s.Height = br.ReadInt32 ' read the biHeight member of the BITMAPINFOHEADER
br.Close()
Return s
Else
Throw New FileNotFoundException()
End If
End Function
End Class
Re: How to read Dimension of .bmp file ??
Hey Frans
thx a lot ya
cheers
Abhilash