将JPG(png/Gif等)转换为64base字符串: [英] Convert a JPG (png / GIf, etc) as a 64base String:

查看:127
本文介绍了将JPG(png/Gif等)转换为64base字符串:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在XP SP3上使用Delphi 7和Indy 9.

I'm using Delphi 7 and Indy 9 on XP SP3.

我有带有多个< img> 标签的HTML.

I have HTML with multiple <img> tags in it.

我想将< img src ="filepath" ...> 标记转换为< img src ="data:image/jpg; base64,..."> 标签.

I want to convert <img src="filepath" ...> tags into <img src="data:image/jpg;base64,..."> tags.

作为练习,我计划在每个图像上都这样做:

As an exercise, I planned to do it on each image like this:

procedure TForm1.Button2Click(Sender: TObject);
VAR
   Str    : AnsiString;
   Stream1, Stream2  : TFileStream;
   Decoder : TIDEncoderMime;

BEGIN
   Stream1 := TFilestream.Create(Curdir + '\solopgang.jpg', fmOpenRead);
   Stream2 := TFilestream.Create('C:\test221.html', fmCreate);
   Try
      Decoder := TIDEncoderMime.Create(NIl);
      Str := decoder.Encode(Stream1^, length(Stream1));
      StrToFile('c:\test221.html',Str);
   Finally
      FreeAndNil(Decoder);
      Stream1.Free;
      Stream2.Free;
  END;
end;

但是,当我在我的 TWebBrowser 中显示它时,我只会得到图像的外框,而我不知道为什么.

But, when I show it in my TWebBrowser, I get only the outer frame of the Image, and I don't know why.

推荐答案

正如David在评论中提到的那样,您的代码未按所示进行编译.它应该更像这样:

As David mentioned in comments, your code does not compile as shown. It should more like this instead:

procedure TForm1.Button2Click(Sender: TObject);
var
  Stream : TFileStream;
  Encoder : TIdEncoderMIME;
begin
  Stream := TFileStream.Create(Curdir + '\solopgang.jpg', fmOpenRead or fmShareDenyWrite);
  try
    Encoder := TIdEncoderMIME.Create(nil);
    try
      StrToFile('c:\test221.html', Encoder.Encode(Stream));
    finally
      Encoder.Free;
    end;
  finally
    Stream.Free;
  end;
end;

如果您升级到Indy 10,则可以改用它:

If you upgrade to Indy 10, you can use this instead:

procedure TForm1.Button2Click(Sender: TObject);
var
  Stream1, Stream2 : TStream;
begin
  Stream1 := TIdReadFileExclusiveStream.Create(Curdir + '\solopgang.jpg');
  try
    Stream2 := TIdFileCreateStream.Create('c:\test221.html');
    try
      Str := TIdEncoderMIME.EncodeStream(Stream1, Stream2);
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;

这篇关于将JPG(png/Gif等)转换为64base字符串:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆