From Clomosy Docs

Revision as of 08:32, 3 June 2024 by ClomosyManager (talk | contribs) (Created page with "FileToStream refers to the process of loading the contents of a file into a stream, specifically a TclMemoryStream. This involves reading the contents of a file and transferring them into a TclMemoryStream instance in memory. This method enables representing the file contents in memory or another data source, allowing for more flexible operations on the content. Clomosy.'''FileToStream'''(AFileName:string):TclMemoryStream; '''Example:'''<br> In the example below, when...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

FileToStream refers to the process of loading the contents of a file into a stream, specifically a TclMemoryStream. This involves reading the contents of a file and transferring them into a TclMemoryStream instance in memory. This method enables representing the file contents in memory or another data source, allowing for more flexible operations on the content.

Clomosy.FileToStream(AFileName:string):TclMemoryStream;

Example:
In the example below, when the application starts, an image is added to the project files using the AddAssetFromUrl function. Then, when a button is clicked, this image is converted to a TclMemoryStream object using the FileToStream function, which returns a TclMemoryStream. This object is then assigned to an image component (Img1).

 var
   Form1 : TCLForm;
   loadImageButton : TClProButton;
   Img1 : TCLImage;
   memoryStream : TCLMemoryStream;
 
 void loadImageButtonClick
 var
   MyFileStr : String;
 {
   MyFileStr = clPathCombine('apple.png', Clomosy.AppFilesPath);
   if clFileExists('apple.png', Clomosy.AppFilesPath) 
   {
     memoryStream = Clomosy.FileToStream(MyFileStr);
     Img1.Bitmap.LoadFromStream(memoryStream); 
     //Img1.Bitmap.LoadFromFile(MyFileStr);
     ShowMessage('Image Uploaded.');
   }
   else 
     ShowMessage('No File');
 }
 
 //------ Main Code -------------
 {
   Form1 = TCLForm.Create(Self);
   Form1.AddAssetFromUrl('https://clomosy.com/demos/apple.png');
   MemoryStream = TclMemoryStream.Create;
   
   Img1 = Form1.AddNewImage(Form1,'Img1');
   Img1.Width = 100;
   Img1.Height = 100;
   
   loadImageButton = Form1.AddNewProButton(Form1,'loadImageButton','Upload Image');
   loadImageButton.Align = alMostBottom;
   loadImageButton.Margins.Top = 20;
   loadImageButton.clProSettings.FontSize = 16;
   loadImageButton.clProSettings.FontColor = clAlphaColor.clHexToColor('#ffffff');
   loadImageButton.clProSettings.BackgroundColor = clAlphaColor.clHexToColor('#6966ff');
   loadImageButton.clProSettings.RoundHeight = 10;
   loadImageButton.clProSettings.RoundHeight = 10;
   loadImageButton.SetclProSettings(loadImageButton.clProSettings);
   Form1.AddNewEvent(loadImageButton,tbeOnClick,'loadImageButtonClick');
   
   Form1.Run;
 }