From Clomosy Docs

No edit summary
No edit summary
Line 1: Line 1:
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.
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
function Clomosy.FileToStream(AFileName:string):TclMemoryStream;
</div>


Clomosy.'''FileToStream'''(AFileName:string):TclMemoryStream;
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.<br>


'''Example:'''<br>
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).


:'''TRObject Syntax'''
<b>Example</b><br>
 
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).<br>
  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;
  }
 
:'''Base Syntax'''


<pre>
  var
  var
   Form1 : TCLForm;
   Form1 : TCLForm;
Line 62: Line 16:
   memoryStream : TCLMemoryStream;
   memoryStream : TCLMemoryStream;
   
   
  procedure loadImageButtonClick
  void loadImageButtonClick
  var
  var
   MyFileStr : String;
   MyFileStr : String;
  begin
  {
   MyFileStr := clPathCombine('apple.png', Clomosy.AppFilesPath);
   MyFileStr = clPathCombine('apple.png', Clomosy.AppFilesPath);
   if clFileExists('apple.png', Clomosy.AppFilesPath) then
   if clFileExists('apple.png', Clomosy.AppFilesPath)  
   begin
   {
     memoryStream := Clomosy.FileToStream(MyFileStr);
     memoryStream = Clomosy.FileToStream(MyFileStr);
     Img1.Bitmap.LoadFromStream(memoryStream);  
     Img1.Bitmap.LoadFromStream(memoryStream);  
     //Img1.Bitmap.LoadFromFile(MyFileStr);
     //Img1.Bitmap.LoadFromFile(MyFileStr);
     ShowMessage('Image Uploaded.');
     ShowMessage('Image Uploaded.');
   end
   }
   else  
   else  
     ShowMessage('No File');
     ShowMessage('No File');
  end;
  }
   
   
  //------ Main Code -------------
  //------ Main Code -------------
  begin
  {
   Form1 := TCLForm.Create(Self);
   Form1 = TCLForm.Create(Self);
   Form1.AddAssetFromUrl('https://clomosy.com/demos/apple.png');
   Form1.AddAssetFromUrl('https://clomosy.com/demos/apple.png');
   MemoryStream := TclMemoryStream.Create;
   MemoryStream = TclMemoryStream.Create;
    
    
   Img1 := Form1.AddNewImage(Form1,'Img1');
   Img1 = Form1.AddNewImage(Form1,'Img1');
   Img1.Width := 100;
   Img1.Width = 100;
   Img1.Height := 100;
   Img1.Height = 100;
    
    
   loadImageButton := Form1.AddNewProButton(Form1,'loadImageButton','Upload Image');
   loadImageButton = Form1.AddNewProButton(Form1,'loadImageButton','Upload Image');
   loadImageButton.Align := alMostBottom;
   loadImageButton.Align = alMostBottom;
   loadImageButton.Margins.Top := 20;
   loadImageButton.Margins.Top = 20;
   loadImageButton.clProSettings.FontSize := 16;
   loadImageButton.clProSettings.FontSize = 16;
   loadImageButton.clProSettings.FontColor := clAlphaColor.clHexToColor('#ffffff');
   loadImageButton.clProSettings.FontColor = clAlphaColor.clHexToColor('#ffffff');
   loadImageButton.clProSettings.BackgroundColor := clAlphaColor.clHexToColor('#6966ff');
   loadImageButton.clProSettings.BackgroundColor = clAlphaColor.clHexToColor('#6966ff');
   loadImageButton.clProSettings.RoundHeight := 10;
   loadImageButton.clProSettings.RoundHeight = 10;
   loadImageButton.clProSettings.RoundHeight := 10;
   loadImageButton.clProSettings.RoundHeight = 10;
   loadImageButton.SetclProSettings(loadImageButton.clProSettings);
   loadImageButton.SetclProSettings(loadImageButton.clProSettings);
   Form1.AddNewEvent(loadImageButton,tbeOnClick,'loadImageButtonClick');
   Form1.AddNewEvent(loadImageButton,tbeOnClick,'loadImageButtonClick');
    
    
   Form1.Run;
   Form1.Run;
  end;
  }
</pre>
 
<h2> See Also </h2>
* [[Clomosy_Class#File_and_Stream_Handling | File and Stream Handling]]

Revision as of 06:10, 25 October 2024

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.


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;
 }

See Also