3. Practical Application 2: Using streams to create executable e-greeting cards
We often see some e-card making software that allows you to choose pictures yourself, and then
It will generate an EXE executable file for you. When you open the greeting card, the picture will be displayed while playing music.
Now that we have learned stream operations, we can also make one.
In the process of adding pictures, we can directly use the previous Cjt_AddtoFile, and what we need to do now is how to
The image is read and displayed. We use the previous Cjt_LoadFromFile to read out the image and save it as a file.
It is also possible to transfer in, but there is a simpler way, which is to directly read the file stream and display it. With
With this powerful tool, everything becomes easier.
The most popular pictures nowadays are BMP format and JPG format. We will now write about these two kinds of pictures
Exit the read and display function.
Function Cjt_BmpLoad(ImgBmp:TImage;SourceFile:String):Boolean;
var
Source:TFileStream;
MyFileSize:integer;
begin
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone);
try
try
Source.Seek(-sizeof(MyFileSize),soFromEnd);
Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));//Read resources
Source.Seek(-MyFileSize,soFromEnd);//Locate the starting position of the resource
ImgBmp.Picture.Bitmap.LoadFromStream(Source);
finally
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
The above is a function for reading BMP images, and the following is a function for reading JPG images. Because JPG units are used, so
To add a sentence to the program: uses jpeg.
Function Cjt_JpgLoad(JpgImg:Timage;SourceFile:String):Boolean;
var
Source:TFileStream;
MyFileSize:integer;
Myjpg: TJpegImage;
begin
try
Myjpg:= TJpegImage.Create;
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone);
try
Source.Seek(-sizeof(MyFileSize),soFromEnd);
Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));
Source.Seek(-MyFileSize,soFromEnd);
Myjpg.LoadFromStream(Source);
JpgImg.Picture.Bitmap.Assign(Myjpg);
finally
Source.Free;
Myjpg.free;
end;
except
Result:=false;
Exit;
end;
Result:=true;
end;
With these two functions, we can make a readout program. Let’s take BMP pictures as an example:
Run Delphi, create a new project, and place an image display control Image1. Create in window
Just write one sentence in the event:
Cjt_BmpLoad(Image1,application.ExeName);
This is the header file, and then we use the previous method to generate a head.res resource file.
Now we can start making our add-on program. The entire code is as follows:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ExtDlgs;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
OpenPictureDialog1: TOpenPictureDialog;
PRocedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Function ExtractRes(ResType, ResName, ResNewName : String):boolean;
Function Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
{Private declarations}
public
{Public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Function TForm1.ExtractRes(ResType, ResName, ResNewName : String):boolean;
var
Res : TResourceStream;
begin
try
Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
try
Res.SavetoFile(ResNewName);
Result:=true;
finally
Res.Free;
end;
except
Result:=false;
end;
end;
Function TForm1.Cjt_AddtoFile(SourceFile,TargetFile:string):Boolean;
var
Target,Source:TFileStream;
MyFileSize:integer;
begin
try
Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareExclusive);
Target:=TFileStream.Create(TargetFile,fmOpenWrite or fmShareExclusive);
try
Target.Seek(0,soFromEnd);//Add resources to the end
Target.CopyFrom(Source,0);
MyFileSize:=Source.Size+Sizeof(MyFileSize);//Calculate the resource size and write it to the end of the auxiliary process
Target.WriteBuffer(MyFileSize,sizeof(MyFileSize));
finally
Target.Free;
Source.Free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Caption:='Bmp2Exe demonstration program. Author: Chen Jingtao';
Edit1.Text:=';
OpenPictureDialog1.DefaultExt := GraphicExtension(TBitmap);
OpenPictureDialog1.Filter := GraphicFilter(TBitmap);
Button1.Caption:='Select BMP picture';
Button2.Caption:='Generate EXE';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
Edit1.Text:=OpenPictureDialog1.FileName;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
HeadTemp:String;
begin
if FileExists(Edit1.Text) then
begin
Application.MessageBox('BMP image file does not exist, please select again!','Message',MB_ICONINFORMATION+MB_OK)
Exit;
end;
HeadTemp:=ChangeFileExt(Edit1.Text,'.exe');
if ExtractRes('exefile','head',HeadTemp) then
if Cjt_AddtoFile(Edit1.Text,HeadTemp) then
Application.MessageBox('EXE file generated successfully!','Message',MB_ICONINFORMATION+MB_OK)
else
begin
if FileExists(HeadTemp) then DeleteFile(HeadTemp);
Application.MessageBox('EXE file generation failed!','Message',MB_ICONINFORMATION+MB_OK)
end;
end;
end.
How about it? It's amazing :) Make the program interface more beautiful and add some functions, you will find that it is better than
It won't be much worse than those software that need to be registered.