ImageEn is one of the best and most popular image libraries for Embarcadero Delphi.
Here is a single function to save an image from TImageEnView as an HTML web page with an optional hyperlink and automatically open it ready to use in your default web browser:
The function first asks for the filename of the HTML Web Page. Then, it prompts the user for an optional Hyperlink URL. Then, it automatically opens the saved HTML Web Page in the user's default web browser. If a HyperLink URL was provided, clicking the image automatically navigates to the provided URL.
Here is a Delphi demo project that contains the demo source and a compiled (signed) executable:
uses
Vcl.Imaging.jpeg, Vcl.Imaging.pngimage, Vcl.Graphics, System.Classes, System.SysUtils,
IdCoderMIME, ImageEnView, Vcl.Dialogs, ShellAPI, Vcl.Forms;
procedure SaveImageAsHTML(ImageEnView: TImageEnView);
var
MemoryStream: TMemoryStream;
Base64Stream: TStringStream;
Base64Encoder: TIdEncoderMIME;
HTMLFile: TStringList;
Base64Image: string;
SaveDialog: TSaveDialog;
JPEGImage: TJPEGImage;
HTMLFileName: string;
ImageLink: string;
LinkProvided: Boolean;
begin
SaveDialog := TSaveDialog.Create(nil);
SaveDialog.DefaultExt := 'html';
SaveDialog.Filter := 'HTML files (*.html)|*.html';
SaveDialog.Options := [ofOverwritePrompt, ofPathMustExist];
try
if not SaveDialog.Execute then
Exit; // User cancelled the save dialog
// Prompt the user to OPTIONALLY enter an image hyperlink:
LinkProvided := InputQuery('OPTIONALLY enter an image hyperlink URL', 'URL:', ImageLink);
// if the user enters no URL (or hits the ESC key) then the image web page is saved normally with no hyperlink
HTMLFileName := SaveDialog.FileName;
MemoryStream := TMemoryStream.Create;
Base64Stream := TStringStream.Create;
Base64Encoder := TIdEncoderMIME.Create(nil);
HTMLFile := TStringList.Create;
JPEGImage := TJPEGImage.Create;
try
// Convert the TImageEnView image to JPEG with 100% quality
JPEGImage.Assign(ImageEnView.Bitmap);
JPEGImage.CompressionQuality := 100;
JPEGImage.SaveToStream(MemoryStream);
MemoryStream.Position := 0;
// Encode the image to Base64
Base64Encoder.Encode(MemoryStream, Base64Stream);
Base64Image := Base64Stream.DataString;
// Create the HTML content
HTMLFile.Add('<html>');
HTMLFile.Add('<head>');
HTMLFile.Add('<style>');
HTMLFile.Add(' body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }');
HTMLFile.Add(' .container { text-align: center; }');
HTMLFile.Add('</style>');
HTMLFile.Add('</head>');
HTMLFile.Add('<body>');
HTMLFile.Add('<div class="container">');
if LinkProvided and (ImageLink <> '') then
HTMLFile.Add('<a href="' + ImageLink + '">');
HTMLFile.Add('<img src="data:image/jpeg;base64,' + Base64Image + '">');
if LinkProvided and (ImageLink <> '') then
HTMLFile.Add('</a>');
HTMLFile.Add('</div>');
HTMLFile.Add('</body>');
HTMLFile.Add('</html>');
// Save the HTML content to a file
HTMLFile.SaveToFile(HTMLFileName);
// Open the saved HTML file in the default browser
ShellExecute(0, 'open', PChar(HTMLFileName), nil, nil, SW_SHOWNORMAL);
finally
MemoryStream.Free;
Base64Stream.Free;
Base64Encoder.Free;
HTMLFile.Free;
JPEGImage.Free;
end;
finally
SaveDialog.Free;
end;
end;
No comments:
Post a Comment