如何将图像转换为base64编码– Delphi 10

2020-09-02



unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls, {Added Units} Vcl.Imaging.jpeg, Vcl.Imaging.pngimage, IdCoderMIME; type TForm1 = class(TForm) edt1: TEdit; btn1: TButton; img1: TImage; grp1: TGroupBox; redt1: TRichEdit; btn2: TButton; dlgOpen1: TOpenDialog; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); begin if dlgOpen1.Execute then begin edt1.Text := dlgOpen1.FileName; img1.Picture.LoadFromFile(edt1.Text); end; end; function Base64_Encoding(FileName: string): string; var stream: TFileStream; base64: TIdEncoderMIME; output: string; begin if (FileExists(FileName)) then begin try begin base64 := TIdEncoderMIME.Create(nil); stream := TFileStream.Create(FileName, fmOpenRead); output := TIdEncoderMIME.EncodeStream(stream); stream.Free; base64.Free; if not(output = '') then begin Result := output; end else begin Result := 'Error'; end; end; except begin Result := 'Error' end; end; end else begin Result := 'Error' end; end; procedure TForm1.btn2Click(Sender: TObject); var dir: string; image: string; FileName: string; extention: string; img_encoded: string; img_html_generate: string; output: string; html_generate: TStringList; begin dir := GetCurrentDir; image := edt1.Text; if (FileExists(image)) then begin FileName := ExtractFileName(image); extention := ExtractFileExt(image); extention := StringReplace(extention, '.', '', [rfReplaceAll, rfIgnoreCase]); FileName := StringReplace(FileName, '.' + extention, '', [rfReplaceAll, rfIgnoreCase]); FileName := StringReplace(FileName, ' ', '', [rfReplaceAll, rfIgnoreCase]); if (extention = 'jpg') or (extention = 'png') then begin try begin img_encoded := Base64_Encoding(image); if not(img_encoded = '') then begin img_html_generate := ' '; redt1.Clear; redt1.Lines.Add(img_html_generate); redt1.Lines.Add('

SwePC Base64 Image Encoder

' + sLineBreak + '

www.swepc.se/blog

'); output := redt1.Text; html_generate := TStringList.Create; try html_generate.Add('' + sLineBreak + '' + sLineBreak + output + sLineBreak + '' + sLineBreak + ''); html_generate.SaveToFile(dir + '\' + 'Base64_img.htm'); finally html_generate.Free; end; MessageBox(Form1.Handle, PChar('Encoding Done and HTML file created!'), PChar('SwePC Base64 Image Encoder'), MB_ICONINFORMATION); end else begin MessageBox(Form1.Handle, PChar('An error has occurred in the program'), PChar('SwePC Base64 Image Encoder'), MB_ICONERROR); end; end; except begin MessageBox(Form1.Handle, PChar('An error has occurred in the program'), PChar('SwePC Base64 Image Encoder'), MB_ICONERROR); end; end; end else begin MessageBox(Form1.Handle, PChar('The File extention is not allowed'), PChar('SwePC Base64 Image Encoder'), MB_ICONWARNING); end; end else begin MessageBox(Form1.Handle, PChar('File not Found'), PChar('SwePC Base64 Image Encoder'), MB_ICONWARNING); end; end; procedure TForm1.FormCreate(Sender: TObject); begin redt1.PlainText := True; redt1.MaxLength := $7FFFFFF0; end; end.





阅读196