bmp流压缩成JPG流
发现我不能回复我以前发的贴子了。郁闷。
http://topic.csdn.net/u/20090825/15/525a7817-6393-47d4-a389-f86b48b069e4.html
这是我的求助贴。
laviewpbt给我的代码我看了下,API大都不懂。不过我用了其中的过程,居然压缩成功了。但是我把压缩后的数据在picture上面显示,是花的。可能是我哪里弄错了。
那个代码肯定是不错的代码,但是,这方面我还是刚学,所以面对那么长串的代码,真的是搞不懂。所以希望能给个单独的使用GdipSaveImageToStream在内存中压缩bmp流的代码。这里可以假设现有的BMP流数组为 picbit()
多谢多谢啊。
[解决办法]
单人只能连续3个回复。你可以发私信让别人帮你回复一下。
[解决办法]
精神可嘉~
[解决办法]
爱莫能助!只能帮顶
[解决办法]
很简单的,你用dephi写个dll控件,代码如下
function CompressionBmpToJpeg(loadFilePath: pchar; saveFilePath: pchar; CompressionQuality: dword): Boolean; stdcall;
var
Bmp: TBitmap;
Jpg: TJpegImage;
MyImage: TMemoryStream; //内存流对象
Buffer: Word;
begin
if not FileExists(loadFilePath) then
begin
Result := False;
Exit;
end;
Bmp := TBitmap.Create;
Jpg := TJpegImage.Create;
MyImage := TMemoryStream.Create; //建立内存流对象
try
MyImage.Position := 0; //移动指针到最开头的位置
MyImage.LoadFromFile(loadFilePath);
MyImage.ReadBuffer(Buffer,2); //读取文件的前2个字节,放到Buffer里面
if Buffer = $4D42 then //如果前两个字节是以4D42[低位到高位] bmp
begin
Bmp.LoadFromFile(loadFilePath);
Jpg.Assign(Bmp);
Jpg.CompressionQuality := CompressionQuality;
Jpg.SaveToFile(saveFilePath);
DeleteFile(loadFilePath);
Result := True;
end
else
begin
Result := False;
end;
finally
Bmp.Free;
Jpg.Free;
MyImage.Free;
end;
end;
-----------------------
然后在放到你的程序里面声明一下调用就行,想怎么搞就怎么搞。可以把2M的相片压缩为30K,图象照样清晰
[解决办法]
两个胖子-------------合肥
[解决办法]
简化了一下第二个函数,去掉了流验证和测试时的字节数组。
'从流转换为图像
Public Function StreamToPicture(ByVal Stream As IStream) As StdPicture
Dim picStream As IStream
Dim lBitmap As Long
Dim hBitmap As Long
Dim lngGdipToken As Long
Dim tPictDesc As PICTDESC
Dim IID_IPicture As IID
Dim oPicture As IPicture
lngGdipToken = StartUpGDIPlus(GdiPlusVersion)
Set picStream = Stream
'从Stream加载Bitmap
If GdipLoadImageFromStream(picStream, lBitmap) = OK Then
'根据Bitmap创建hBitbmp
If GdipCreateHBITMAPFromBitmap(lBitmap, hBitmap, 0) = OK Then
With tPictDesc
.cbSizeOfStruct = Len(tPictDesc)
.picType = vbPicTypeBitmap
.hgdiObj = hBitmap
.hPalOrXYExt = 0
End With
' 初始化IPicture
With IID_IPicture
.Data1 = &H7BF80981
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(3) = &HAA
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
Call OleCreatePictureIndirect(tPictDesc, IID_IPicture, True, oPicture)
Set StreamToPicture = oPicture
End If
End If
Set picStream = Nothing
GdiplusShutdown lngGdipToken
End Function