本文介绍了使用Delphi 7 BASE64函数可显示< img src ="数据:image/jpg;数据,xxxx-/>细绳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Delphi7中实现此功能

I'm trying to make this function in Delphi7

Function CreateBase64String(FileName:String):Base64String; 

尝试了INDY 6-7和9-但是INDY(6-7--9)除了错误外没有给我任何帮助.不知道为什么.

Have tried INDY 6-7 and 9 - but INDY (6-7--9) gives me nothing but errors. Don't know why.

经过很多麻烦,我终于做到了:

After a lot of trouble I have finally made this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, ExtDlgs, StdCtrls, Jpeg;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    select: TButton;
    Label1: TLabel;
    Label2: TLabel;
    ToB64: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    DecBtn: TButton;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure ToB64Click(Sender: TObject);
    procedure selectClick(Sender: TObject);
    procedure DecBtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1  : TForm1;
  Curdir : string;

implementation

Uses
   EncdDeCd;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
   Curdir := ExtractfileDir(Application.ExeName);
end;

procedure TForm1.ToB64Click(Sender: TObject);
var
   instream, oustream :  TMemoryStream;
begin
   instream := TMemoryStream.Create;
   instream.LoadFromFile(Edit1.text);
   oustream := TMemoryStream.Create;
   EncodeStream(instream,oustream);
   instream.Free;
   oustream.SaveToFile(Edit2.Text);
   oustream.Free;
end;

procedure TForm1.selectClick(Sender: TObject);
begin
   Edit1.Text := '';
   if OpenPictureDialog1.Execute then
      begin
         Edit1.Text := OpenPictureDialog1.FileName;
         Edit2.Text := ChangeFileExt(Edit1.Text,'.b64');
      end;
end;

procedure TForm1.DecBtnClick(Sender: TObject);
var
   instream, oustream : TMemoryStream;
   s                  : string;
begin
   instream := TMemoryStream.Create;
   instream.LoadFromFile(Edit2.text); //b64 file
   oustream := TMemoryStream.Create;
   DecodeStream(instream,oustream);
   instream.Free;
   s := Edit1.Text;
   insert('x',s,pos('.',s));        // sæt et 'X' på org filen
   oustream.SaveToFile(s);
   oustream.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR
   Ofile : TextFile;
   ifile : TextFile;
   Istr  : string;
begin
   Edit1.Text := Curdir + '\sunset.jpg';
   Edit2.Text := Curdir + '\sunset.b64';
   ToB64Click(NIL);
   AssignFile(Ofile,curdir + '\TESTABCDEF.HTML');
   Rewrite(ofile);
   Writeln(ofile,'<HTML><BODY>');
   Write(ofile,'<img src="DATA:IMAGE/JPG; base64,');
   Assignfile(ifile,edit2.Text);
   Reset(ifile);
   While not Eof(Ifile) DO
      BEGIN
         Readln(Ifile,Istr);
         Write(ofile,Istr);
      END;
   Closefile(ifile);
   Writeln(ofile, '" height="100" width="100" />');
   Writeln(Ofile,'</body></html>');
   Closefile(ofile);
   DecBtnClick(NIL);
end;

END.

但是,无论我做什么,OFILE(TESTABCDEF.HTML)都无法正确显示在我的Web浏览器(Firefox和IE8)中.

But no matter what I do, the OFILE (TESTABCDEF.HTML) don't show up correctly in my webbrowsers (Firefox and IE8).

AssignFile-stuff仅用于测试目的.通常我使用Streams.

The AssignFile-stuff is for test purposes only. Normally I use Streams.

尝试使用大JPEG->错误尝试了小JPEG->错误!

Tried big JPEG's --> ErrorTried small JPEG's --> Error!

有人可以告诉我我在做什么错吗?

Can anybody tell me What I'm doing wrong?

克里斯(Kris)/挪威

Kris / Norway

推荐答案

Indy的base64编码器工作正常,您可能只是未正确使用它.

Indy's base64 encoder works just fine, you probably just are not using it correctly.

Uses
  ..., IdCoderMIME;

Function CreateBase64String(FileName: String): String; 
var
  Fs: TFileStream;
begin
  Fs := TFileStream.Create(FileName, fmOpenRead);
  try
    Result := TIdEncoderMIME.EncodeStream(Fs);
  finally
    Fs.Free;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  Ofile : TextFile;
begin
  AssignFile(Ofile,curdir + '\TESTABCDEF.HTML');
  Rewrite(ofile);
  Writeln(ofile,'<HTML><BODY>');
  Write(ofile,'<img src="data:image/jpeg;base64,');
  Write(ofile, CreateBase64String(Curdir + '\sunset.jpg'));
  Writeln(ofile, '" height="100" width="100" />');
  Writeln(Ofile,'</body></html>');
  Closefile(ofile);
end;

我什至会尽可能地摆脱 TextFile 并改用 TFileStream ,以便Indy可以将其base64输出直接写入目标文件,而不必首先浪费内存,将其放在 String 中:

I would even go as far as getting rid of the TextFile and use a TFileStream instead, so that Indy can write its base64 output directly to your target file and not waste memory putting it in a String first:

Uses
  ..., IdCoderMIME;

procedure WriteBase64String(ADest: TStream; FileName: String);
var
  Fs: TFileStream;
begin
  Fs := TFileStream.Create(FileName, fmOpenRead);
  try
    TIdEncoderMIME.EncodeStream(Fs, ADest);
  finally
    Fs.Free;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  Ofile : TFileStream;
begin
  Ofile := TFileStream.Create(curdir + '\TESTABCDEF.HTML', fmCreate);
  try
    // WriteStringToStream() is an Indy function...
    WriteStringToStream(Ofile, '<HTML><BODY>'+EOL);
    WriteStringToString(Ofile, '<img src="data:image/jpeg;base64,');
    WriteBase64String(Ofile, Curdir + '\sunset.jpg');
    WriteStringToStream(Ofile, '" height="100" width="100" />'+EOL);
    WriteStringToStream(Ofile, '</body></html>'+EOL);
  finally
    Ofile.Free;
  end;
end;

这篇关于使用Delphi 7 BASE64函数可显示&lt; img src =&quot;数据:image/jpg;数据,xxxx-/&gt;细绳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:54