Results 1 to 7 of 7

Thread: [Resolved] How to read Dimension of .bmp file ??

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Resolved [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
    Last edited by Abhilash007; Apr 4th, 2007 at 02:40 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to read Dimension of .bmp file ??

    Moved from Application Deployment forum.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    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

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    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

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to read Dimension of .bmp file ??

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    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
    Last edited by Frans C; Mar 30th, 2007 at 06:15 AM.
    Frans

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: How to read Dimension of .bmp file ??

    Hey Frans

    thx a lot ya


    cheers
    Abhilash

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width