如何对位图数组进行压缩?

frequent 2007-12-07 07:39:59
一个网络传输图片的程序,求助。

已将位图保存在数组中,如放在数组a中。
其中a(0)保存第一象素的R值,a(1)保存第一象素的G值,a(2)保存第一象素的B值,a(3)保存第二象素的R值,。。。。。以此类推将位图数据完全保存在了数组中。
现在想对这个数组进行压缩。想把他转换为JPG格式的,但网上提供的DLL都是对文件进行操作的。我不想操作文件。直接转换为JPG,然后再把它在网络上传输给对方后再还原为原来的位图数组(当然肯定也不能完全还原为原来的数组了,BMP转换为JPG肯定会有失真的,只要还原后的位图数组画出来的图有JPG的逼真度就可以了)

求大家给个思路。
或者提供BMP转换为JPG的算法也行,谢谢了。
...全文
21 点赞 收藏 1
写回复
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jf110 2007-12-07
BMP转换为JPG

Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
GUID As GUID
NumberOfvalues As Long
type As Long
value As Long
End Type
Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type
Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hpal As Long, Bitmap As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal FileName As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long

' ----==== SaveJPG ====----

Public Sub SaveJPG(ByVal pict As StdPicture, ByVal FileName As String, Optional ByVal quality As Byte = 80)
Dim tSI As GdiplusStartupInput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long
' Initialize GDI+
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI)
If lRes = 0 Then
' Create the GDI+ bitmap
' from the image handle
lRes = GdipCreateBitmapFromHBITMAP(pict.Handle, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
' Initialize the encoder GUID
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
' Initialize the encoder parameters
tParams.Count = 1
With tParams.Parameter ' Quality
' Set the Quality GUID
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB3505E7EB}"), .GUID
.NumberOfvalues = 1
.type = 1
.value = VarPtr(quality)
End With
' Save the image
lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileName), tJpgEncoder, tParams)
' Destroy the bitmap
GdipDisposeImage lBitmap
End If
' Shutdown GDI+
GdiplusShutdown lGDIP
End If
If lRes Then
'Err.Raise 5, , "Cannot save the image. GDI+ Error:" & lRes
GdiplusShutdown lGDIP
lRes = GdiplusStartup(lGDIP, tSI)

End If
End Sub

回复
发动态
发帖子
VB基础类
创建于2007-09-28

7450

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告