//*****************************************
//函数: BmpToHex
//作者: 不得闲
//QQ: 75492895
//email: appleak46@yahoo.com.cn
//功能:通过Bitmap句柄将位图转换为16进制
//请大家尊重作者劳动成果,转载请保留作者信息,谢谢
//*****************************************
function BmpToHex(BmpHandle: HBITMAP): string;
var
ds: TDIBSection;
BmpFileHeader: TBitmapFileHeader;
NumberOfColors,BitCount: Integer;
Bm: tagBITMAP;
Bytes: integer;
begin
Bytes := GetObject(BmpHandle,SizeOf(ds),@ds); //获得有关DIBSECTION结构中的点阵图资讯
if Bytes = 0 then raise Exception.Create(‘无效的位图‘);
Bytes := GetObject(BmpHandle,SizeOf(bm),@Bm); //获得tagBITMAP结构
if Bytes = 0 then raise Exception.Create(‘无效的位图‘);
NumberOfColors := ds.dsBmih.biClrUsed; //获得调色板中实际使用的颜色数
BitCount := ds.dsBmih.biBitCount; //位图位数
if (NumberOfColors = 0) and (BitCount <= 8) then
NumberOfColors := 1 shl BitCount;
With BmpFileHeader do
begin
bfType := $4D42; // ‘BM‘位图标记
bfReserved1 := 0;
bfReserved2 := 0;
bfOffBits := SizeOf(TBitmapFileHeader) +
SizeOf(TBitmapInfoHeader) +
NumberOfColors*SizeOf(TRGBQuad); //获得信息头大小
bfSize := bfOffBits + ds.dsBmih.biSizeImage; //获得文件大小
end;
Result := PointToHex(@BmpFileHeader,SizeOf(BmpFileHeader)) + PointToHex(@ds.dsBmih,SizeOf(ds.dsBmih)) + PointToHex(bm.bmBits,ds.dsBmih.biSizeImage);
end;
//*****************************************
//函数: BmpHexToBmp
//作者: 不得闲
//QQ: 75492895
//email: appleak46@yahoo.com.cn
//功能:通过BmpToHex的16进制串转化成位图数据
//请大家尊重作者劳动成果,转载请保留作者信息,谢谢
//*****************************************
procedure BmpHexToBmp(const HexStr: string;bmp: TBitmap);
var
Stream: TMemoryStream;
begin
if bmp = nil then
raise Exception.Create(‘位图不能为空‘);
Stream := TMemoryStream.Create;
Stream.SetSize(Length(HexStr)div 2);
Classes.HexToBin(Pchar(HexStr),Pchar(Integer(Stream.Memory)),Stream.Size);
Stream.Seek(0,soFromBeginning);
bmp.LoadFromStream(Stream);
Stream.Free;
end;
请看以前的文章PointToHex
HexToBin,如果偶写的那个不行(可能需要修改一下),请换成Classes中的那个