procedure TForm1.Button1Click(Sender: TObject); var opdlg:topendialog; picpath:string; picStream:TMemoryStream ; PicData:Byte; PicSize,index:integer; tempstr:string; tempbitmap:TBitmap; tempjpeg:TJPEGImage; tempstream:TMemoryStream; begin try ProgressBar1.Position:=0; opdlg:=TOpenDialog.Create(nil); tempbitmap:=TBitmap.Create; tempjpeg:=TJPEGImage.Create; picstream:=TMemoryStream.Create; picpath:=''; if opdlg.Execute then begin picpath:=opdlg.FileName; self.Caption:='Load file: '+picpath; end; if picpath>'' then begin picstream.LoadFromFile(picpath); if pos('.bmp',picpath)>0 then image1.Picture.LoadFromFile(picpath) else if pos('.jpg',picpath)>0 then begin tempjpeg.LoadFromFile(picpath); tempbitmap.Assign(tempjpeg); Image1.Picture.Assign(tempbitmap); end else begin self.Caption:='invaid image format! Exit process...'; exit; end; picstream.Position:=0; picsize:=picStream.Size; ProgressBar1.Max:=PicSize; memo1.lines.Clear; application.ProcessMessages; memo1.lines.BeginUpdate; tempstr:=''; label1.Caption:='正在处理数据...'; label2.Caption:='图片大小为:'+formatfloat('0,000',picsize)+' Bytes'; self.Update; sleep(200); for index := 0 to PicSize do begin picStream.Read(picdata,1); tempstr:=tempstr+' '+inttohex(picdata,2); if (index+1) mod 50=0 then begin memo1.lines.Add(tempstr); tempstr:=''; end; ProgressBar1.Position:=index; application.ProcessMessages; end; if tempstr>'' then memo1.lines.Add(tempstr); label1.Caption:='read data completed!'; memo1.lines.EndUpdate; ProgressBar1.Position:=0; end; finally freeandnil(picstream); freeandnil(tempbitmap); FreeAndNil(tempjpeg); freeandnil(opdlg); end; end; 这里的代码是把图片放到流里,然后把它按字节显示成十六进制的字符串。 至于你所讲的。要把十六进制的字符串显示成图片。方法其实就是与上而后相反了。流的READ方法就变成WRITE()方法。LOADFROMFILE()换成SAVETOFILE()方法。 具体代码我就不写了,算是给你自己一个练习机会吧。^______^注:上面的代码我是通过调试的。
tmemorystream.loadfromfile()...
tmemorystream.loadfromstream()...
tmemorystream.write()....
tmemorystream.read()....
能不能写得详细点呢?
var
opdlg:topendialog;
picpath:string;
picStream:TMemoryStream ;
PicData:Byte;
PicSize,index:integer;
tempstr:string;
tempbitmap:TBitmap;
tempjpeg:TJPEGImage;
tempstream:TMemoryStream;
begin
try
ProgressBar1.Position:=0;
opdlg:=TOpenDialog.Create(nil);
tempbitmap:=TBitmap.Create;
tempjpeg:=TJPEGImage.Create;
picstream:=TMemoryStream.Create;
picpath:='';
if opdlg.Execute then
begin
picpath:=opdlg.FileName;
self.Caption:='Load file: '+picpath;
end;
if picpath>'' then
begin picstream.LoadFromFile(picpath);
if pos('.bmp',picpath)>0 then
image1.Picture.LoadFromFile(picpath)
else
if pos('.jpg',picpath)>0 then
begin
tempjpeg.LoadFromFile(picpath);
tempbitmap.Assign(tempjpeg);
Image1.Picture.Assign(tempbitmap);
end
else
begin
self.Caption:='invaid image format! Exit process...';
exit;
end; picstream.Position:=0;
picsize:=picStream.Size;
ProgressBar1.Max:=PicSize;
memo1.lines.Clear;
application.ProcessMessages;
memo1.lines.BeginUpdate;
tempstr:='';
label1.Caption:='正在处理数据...';
label2.Caption:='图片大小为:'+formatfloat('0,000',picsize)+' Bytes';
self.Update;
sleep(200);
for index := 0 to PicSize do begin
picStream.Read(picdata,1);
tempstr:=tempstr+' '+inttohex(picdata,2);
if (index+1) mod 50=0 then
begin
memo1.lines.Add(tempstr);
tempstr:='';
end;
ProgressBar1.Position:=index;
application.ProcessMessages;
end;
if tempstr>'' then
memo1.lines.Add(tempstr);
label1.Caption:='read data completed!';
memo1.lines.EndUpdate;
ProgressBar1.Position:=0;
end; finally
freeandnil(picstream);
freeandnil(tempbitmap);
FreeAndNil(tempjpeg);
freeandnil(opdlg);
end;
end;
这里的代码是把图片放到流里,然后把它按字节显示成十六进制的字符串。
至于你所讲的。要把十六进制的字符串显示成图片。方法其实就是与上而后相反了。流的READ方法就变成WRITE()方法。LOADFROMFILE()换成SAVETOFILE()方法。
具体代码我就不写了,算是给你自己一个练习机会吧。^______^注:上面的代码我是通过调试的。
1、创建一个内存流(TMemoryStream)对象;
2、调用 Image1.Picture.Bitmap.SaveToStream 将位图复制到内存流中;
3、从内存流中读出每个字节,用 IntToHex 变成两位十六进制串,写到文件中。读文件:
1、打开文本文件;
2、创建一个内存流(TMemoryStream)对象;
3、每次从文本文件读入两个字符,用 StrToHex( '$' + 两个字符 ) 转换成字节;
4、将字节写到内存流中;
5、所有字节完成后,调用 Image1.Picture.Bitmap.LoadFromStream 将内存流转换为位图。善后工作要做好。
多谢你的提示~~~