首页 > 代码库 > 将图片以字符串方式保存
将图片以字符串方式保存
将图片转换成字符
delphi的*.dfm文件
mht的文件
//------------------------------------------------------------------------------
//jpg转换为 txt字符串
//jpg转换为 txt字符串
//JpegToText(‘C:\1.jpg‘, ‘C:\1.txt‘);
function JpegToText(jpgFileName: string; TxtFileName: string): string;
var
FileStream: TFileStream;
StringStream: TStringStream;
FSize: integer;
FileData: string;
i: integer;
DataStr: string;
begin
try
FileStream := TFileStream.Create(jpgFileName, fmOpenRead);
StringStream := TStringStream.Create(EmptyStr);
FSize := FileStream.Size;
StringStream.CopyFrom(FileStream, FSize);
FileData := StringStream.DataString;
DataStr := EmptyStr;
for i := 1 to Length(FileData) do
begin
DataStr := DataStr + InttoHex(Ord(FileData[i]), 2);
end;
Result := DataStr;
finally
freeandnil(FileStream);
freeandnil(StringStream);
end;
end;
//------------------------------------------------------------------------------
//TextToJpeg(‘C:\1.txt‘, ‘C:\2.jpg‘);
function JpegToText(jpgFileName: string; TxtFileName: string): string;
var
FileStream: TFileStream;
StringStream: TStringStream;
FSize: integer;
FileData: string;
i: integer;
DataStr: string;
begin
try
FileStream := TFileStream.Create(jpgFileName, fmOpenRead);
StringStream := TStringStream.Create(EmptyStr);
FSize := FileStream.Size;
StringStream.CopyFrom(FileStream, FSize);
FileData := StringStream.DataString;
DataStr := EmptyStr;
for i := 1 to Length(FileData) do
begin
DataStr := DataStr + InttoHex(Ord(FileData[i]), 2);
end;
Result := DataStr;
finally
freeandnil(FileStream);
freeandnil(StringStream);
end;
end;
//------------------------------------------------------------------------------
//TextToJpeg(‘C:\1.txt‘, ‘C:\2.jpg‘);
function TextToJpeg(TxtFileName: string; JpegFileName: string): TPicture;
var
buf: array of Byte;
DataLength: Integer;
i: Integer;
DataStr: string;
ByteFile: file of byte;
tmpFileName: string;
begin
with TStringList.Create do
begin
LoadFromFile(TxtFileName);
DataStr := Text;
Free;
end;
DataLength := Length(DataStr) div 2 - 1;
SetLength(buf, DataLength);
for i := 1 to DataLength do
begin
buf[i - 1] := StrToInt(‘$‘ + copy(DataStr, (i - 1) * 2 + 1, 2));
end;
tmpFileName := ExtractFilePath(ParamStr(0)) + ‘tmp.jpg‘;
AssignFile(ByteFile, tmpFileName);
Rewrite(ByteFile);
for i := 0 to DataLength - 1 do
begin
write(ByteFile, buf[i]);
end;
CloseFile(ByteFile);
Result:=TPicture.Create;
Result.LoadFromFile(tmpFileName); //慢
DeleteFile(tmpFileName);
end;
var
buf: array of Byte;
DataLength: Integer;
i: Integer;
DataStr: string;
ByteFile: file of byte;
tmpFileName: string;
begin
with TStringList.Create do
begin
LoadFromFile(TxtFileName);
DataStr := Text;
Free;
end;
DataLength := Length(DataStr) div 2 - 1;
SetLength(buf, DataLength);
for i := 1 to DataLength do
begin
buf[i - 1] := StrToInt(‘$‘ + copy(DataStr, (i - 1) * 2 + 1, 2));
end;
tmpFileName := ExtractFilePath(ParamStr(0)) + ‘tmp.jpg‘;
AssignFile(ByteFile, tmpFileName);
Rewrite(ByteFile);
for i := 0 to DataLength - 1 do
begin
write(ByteFile, buf[i]);
end;
CloseFile(ByteFile);
Result:=TPicture.Create;
Result.LoadFromFile(tmpFileName); //慢
DeleteFile(tmpFileName);
end;
//jpg转换为 txt字符串 并保存为txt
procedure SaveJpgToTextFile(jpgFileName: string; TxtFileName: string);
begin
with TStringList.Create do
begin
Text := JpegToText(jpgFileName,TxtFileName);
SaveToFile(TxtFileName);
Free;
end;
end;
procedure SavePictureToFile(pitcture:TPicture;FileName:string);
var
pic:TPicture;
begin
pic:=TPicture.Create;
pic:=pitcture;
pic.SaveToFile(FileName);
// pic.Free;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
SavePictureToFile(Image1.Picture,‘C:\3.jpg‘);
end;
var
pic:TPicture;
begin
pic:=TPicture.Create;
pic:=pitcture;
pic.SaveToFile(FileName);
// pic.Free;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
SavePictureToFile(Image1.Picture,‘C:\3.jpg‘);
end;
来自为知笔记(Wiz)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。