CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Mar 2002
    Posts
    12

    How to convert bmp image to jpg image?

    Hi,

    I am using following code to create bmp image.

    image.EditCopy
    SavePicture Clipboard.GetData, "C:\tmp.bmp"

    So, I have the image as tmp.bmp file. Now, I need to convert this bmp image to jpg image.

    I am currently using one method that converts bmp to jpg provided by one of the VB source site.

    But, this method fails when I run my program as a scheduled task. So, I want to try out an alternative method which would convert bmp image to jpg image.

    Any input would be helpful.

    Thank you,

    Regards,
    Swati


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How to convert bmp image to jpg image?


    Convert BMP to JPG with this code. (Note: Requires vic32.dll available from
    http://www.catenary.com/)

    'PLACE ALL THIS IN A new MODULE

    Declare Function bmpinfo Lib "VIC32.DLL" (byval Fname as string, bdat as BITMAPINFOHEADER) as Long
    Declare Function allocimage Lib "VIC32.DLL" (image as imgdes, byval wid as Long, byval leng as Long, byval BPPixel as Long) as Long
    Declare Function loadbmp Lib "VIC32.DLL" (byval Fname as string, desimg as imgdes) as Long
    Declare Sub freeimage Lib "VIC32.DLL" (image as imgdes)
    Declare Function convert1bitto8bit Lib "VIC32.DLL" (srcimg as imgdes, desimg as imgdes) as Long
    Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg as imgdes, desimg as imgdes)
    Declare Function savejpg Lib "VIC32.DLL" (byval Fname as string, srcimg as imgdes, byval quality as Long) as Long



    ' Image descriptor
    Type imgdes
    ibuff as Long
    stx as Long
    sty as Long
    endx as Long
    endy as Long
    buffwidth as Long
    palette as Long
    colors as Long
    imgtype as Long
    bmh as Long
    hBitmap as Long
    End Type

    Type BITMAPINFOHEADER
    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

    'PLACE THIS IN YOUR FORM DECLERATIONS

    private Sub ConvertToJPEG(bmp_fname as string, jpg_fname as string, optional quality as Long)
    Dim tmpimage as imgdes ' Image descriptors
    Dim tmp2image as imgdes
    Dim rcode as Long
    'Dim quality as Long
    Dim vbitcount as Long
    Dim bdat as BITMAPINFOHEADER ' Reserve space for BMP struct
    'Dim bmp_fname as string
    'Dim jpg_fname as string

    'bmp_fname = "test.bmp"
    'jpg_fname = "test.jpg"

    If quality = 0 then quality = 75

    ' get info on the file we're to load
    rcode = bmpinfo(bmp_fname, bdat)
    If (rcode <> NO_ERROR) then
    msgbox "error: Unable to get file info"
    Exit Sub
    End If

    vbitcount = bdat.biBitCount
    If (vbitcount >= 16) then ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer
    vbitcount = 24
    End If

    ' Allocate space for an image
    rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount)
    If (rcode <> NO_ERROR) then
    msgbox "error: Not enough memory"
    Exit Sub
    End If

    ' Load image
    rcode = loadbmp(bmp_fname, tmpimage)
    If (rcode <> NO_ERROR) then
    freeimage tmpimage ' Free image on error
    msgbox "error: Cannot load file"
    Exit Sub
    End If

    If (vbitcount = 1) then ' If we loaded a 1-bit image, convert to 8-bit grayscale
    ' because jpeg only supports 8-bit grayscale or 24-bit color images
    rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8)
    If (rcode = NO_ERROR) then
    rcode = convert1bitto8bit(tmpimage, tmp2image)
    freeimage tmpimage ' Replace 1-bit image with grayscale image
    copyimgdes tmp2image, tmpimage
    End If
    End If

    ' Save image
    rcode = savejpg(jpg_fname, tmpimage, quality)
    freeimage tmpimage
    Kill bmp_fname
    msgbox "picture saved: " & jpg_fname

    End Sub




    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    Apr 2013
    Posts
    2

    Re: How to convert bmp image to jpg image?

    convert this bmp image to jpg image can use this method, you can give a look at this image programme.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to convert bmp image to jpg image?

    Note that this is a 11 year old thread and the supposed answer just given in post #3 is not in VB6

    Please do not revive old threads and do not post VB.Net solutions for VB6 issues, these are two different things.
    Always use [code][/code] tags when posting code.

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

Featured