将图片以字符串方式保存

将图片转换成字符
 
delphi的*.dfm文件
mht的文件
 
//------------------------------------------------------------------------------
//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 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;
 
 
 
//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; 
 
 
 





你可能感兴趣的:(字符串)