【问题标题】:How to convert bmp to jpg in vb6vb6如何将bmp转为jpg
【发布时间】:2011-05-29 20:49:13
【问题描述】:

vb6如何将bmp转为jpg?

【问题讨论】:

标签: image vb6 bitmap jpeg


【解决方案1】:

检查这个link

    '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

【讨论】:

  • VIC32.DLL 只有大约 250 kb。维克多图书馆的价格仅为 499 美元,没有发行版税
【解决方案2】:

From here:

虽然有各种第三方控件可用于以其他格式保存文件,但这些控件通常具有不可接受的“每席位”许可政策,并且可能非常昂贵。本文介绍如何使用免费的 JPEG DLL 库保存 VB 图片。

PS:为了保存 jpg 图像而花 500 美元购买像 VIC32 这样的库似乎太多了......

【讨论】:

    【解决方案3】:

    使用ImageMagick 发送它并执行类似的操作

    shell "convert.exe image.bmp image.jpg"
    

    【讨论】:

    • 当文件在网络共享中时它不起作用。我该如何解决这个问题?
    • @Rick 先将它们复制到本地驱动器?我不知道 ImageMagick 不能处理网络共享中的文件。我对网络共享没有任何经验。
    【解决方案4】:

    您可以从VB6use GDI+保存为JPG。

    【讨论】:

    • 警告:GDI+ 在 Windows 服务中不安全,在 IIS 下运行似乎也不安全。
    【解决方案5】:

    嗯,对于 XP SP1 及更高版本,您可以使用提供的工具:WIA 2.0 库。

    要简单地将 BMP 转换为 JPG,您可以在此处删除近一半的行:

    Option Explicit
    '
    'Requires a reference to:
    '   Microsoft Windows Image Acquisition Library v2.0
    '
    
    Private Const TIFF_LZW As String = "LZW"
    Private Const TIFF_RLE As String = "RLE"       'Pixel Depth must be 1.
    Private Const TIFF_CCITT3 As String = "CCITT3" 'Pixel Depth must be 1.
    Private Const TIFF_CCITT4 As String = "CCITT4" 'Pixel Depth must be 1.
    Private Const TIFF_Uncompressed As String = "Uncompressed"
    
    Private Sub ImgConvert( _
        ByVal InFileName As String, _
        ByVal OutFileName As String, _
        ByVal OutFormat As String, _
        Optional ByVal Quality As Integer = 100, _
        Optional ByVal Compression As String = TIFF_LZW)
    
        Dim Img As WIA.ImageFile
        Dim ImgProc As WIA.ImageProcess
    
        Set Img = New WIA.ImageFile
        Img.LoadFile InFileName
        Set ImgProc = New WIA.ImageProcess
        With ImgProc.Filters
            .Add ImgProc.FilterInfos("Convert").FilterID
            .Item(1).Properties("FormatID").Value = OutFormat
            If OutFormat = wiaFormatJPEG Then
                .Item(1).Properties("Quality").Value = Quality
            ElseIf OutFormat = wiaFormatTIFF Then
                .Item(1).Properties("Compression").Value = Compression
            End If
        End With
        Set Img = ImgProc.Apply(Img)
    
        On Error Resume Next
        Kill OutFileName
        On Error GoTo 0
        Img.SaveFile OutFileName
    End Sub
    
    Private Sub Main()
        ImgConvert "sample.bmp", "sample.jpg", wiaFormatJPEG, 70
        ImgConvert "sample.bmp", "sample.gif", wiaFormatGIF
        ImgConvert "sample.bmp", "sample.png", wiaFormatPNG
        ImgConvert "sample.bmp", "sample.tif", wiaFormatTIFF, , TIFF_Uncompressed
        MsgBox "Complete"
    End Sub
    

    对于 XP,您需要部署它:Windows® Image Acquisition Automation Library v2.0 Tool: Image acquisition and manipulation component for VB and scripting

    对于以后的任何事情,它已经是操作系统的一部分。

    【讨论】:

    • 在 XP 中必须手动注册 DLL,对吗?除了注册还有其他选择吗?
    • @Rick 您可以将其作为问题发布
    • 当您从 SDK 的 ZIP 存档中获取它时,您需要将 WIAAut.dll 复制到 System32 并注册它。但是对于重新分发,您可以使用任何安装程序/安装程序技术来部署它,这应该像任何 ActiveX 库一样安装和注册它。
    • 优秀的答案,不需要额外的 DLL(假设 XP SP1 或更高版本)!请注意,此代码在 Microsoft Access 中完美运行。谢谢!
    【解决方案6】:

    简单!
    1) 这是 SHORTEST 方法:

    SavePicture(LoadPicture("c:\YourFile.BMP"),"c:\YourFile.JPG")
    

    2) 如果你想转换成任何格式(带/不带压缩),我推荐FFMpeg

    Shell("ffmpeg.exe -i YourFile.bmp -q <qualityNumber*> ConverTo.Any")
    

    *写ffmpeg /?在cmd中了解用法

    【讨论】:

    • 虽然确实在技术上更改了扩展名,但实际上并没有将文件转换为 .jpg。它只是一个重命名的位图(据此:msdn.microsoft.com/en-us/library/aa445827%28v=vs.60%29.aspx)。
    • @nerdherd,我明白了。但他说的是“转换”,而不是压缩转换;)
    • 只是想确保其他读者知道。 +1 添加替代选项以获得更有用的转换。
    猜你喜欢
    • 2012-12-26
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2012-08-13
    相关资源
    最近更新 更多