From Clomosy Docs

No edit summary
No edit summary
Line 28: Line 28:
|-
|-
| LoadFromFile ||testImg.Bitmap.LoadFromFile('C:\Users\Administrator\Desktop\logo.png') || The LoadFromFile method in Clomosy is used to load an image file and transfer it to a bitmap object. In the example, the expression testImg.Bitmap.LoadFromFile loads an image from a file path into the Bitmap property of a component named testImg.
| LoadFromFile ||testImg.Bitmap.LoadFromFile('C:\Users\Administrator\Desktop\logo.png') || The LoadFromFile method in Clomosy is used to load an image file and transfer it to a bitmap object. In the example, the expression testImg.Bitmap.LoadFromFile loads an image from a file path into the Bitmap property of a component named testImg.
|-
|SaveToStream || testImg.Bitmap.SaveToStream(FileStream); //FileStream:TCLFileStream;  || Bitmap image (TclImage) transfer to TCLFileStream. See the [[TclFileStream | page]] to learn about these features.
|-
|LoadFromStream || testImg.Bitmap.LoadFromStream(FileStream); //FileStream:TCLFileStream; || Transferring TCLFileStream data to bitmap image (TclImage). See the [[TclFileStream | page]] to learn about these features.
|}
|}


Line 71: Line 75:




'''Example 2:'''<br>
'''Example 2(LoadFromFile):'''<br>
In the example, a TclForm form is created and an image is downloaded from the internet to display on this form. First, a new TclForm instance named Form1 is created. The Form1.AddAssetFromUrl method is used to download an image from the specified URL to the project’s file directory. Then, a new TclImage instance is added to the form and configured to cover the entire form. The path of the downloaded image file is obtained using Clomosy.AppFilesPath and the Image1.Bitmap.LoadFromFile method is used to load the image.
In the example, a TclForm form is created and an image is downloaded from the internet to display on this form. First, a new TclForm instance named Form1 is created. The Form1.AddAssetFromUrl method is used to download an image from the specified URL to the project’s file directory. Then, a new TclImage instance is added to the form and configured to cover the entire form. The path of the downloaded image file is obtained using Clomosy.AppFilesPath and the Image1.Bitmap.LoadFromFile method is used to load the image.


Line 111: Line 115:
     ShowMessage('An error was encountered while uploading the file.');
     ShowMessage('An error was encountered while uploading the file.');
   end;
   end;
end;
</pre>
'''Example 3 (Save image to file) :'''<br>
The example creates a form (TclForm) and an image component (TclImage) and implements a function to load an image from a specific URL and save it to a file. First, Form1 and Image1 objects are created, and the image component is added to the form. The Image1 component is updated with a background image from the specified URL (https://clomosy.com/educa/bg2.png). The SaveFile procedure uses TCLMemoryStream to save the bitmap of Image1 to a memory stream and writes this data to a file (newImgBG.png) in Base64 format. The SaveFile procedure is linked to the click event of Image1, so when the form is run and the user clicks the image, the image is saved to the file. If any errors occur, an error message is displayed.
:'''TRObject Syntax'''
<pre>
var
  Form1: TclForm;
  Image1: TclImage;
  MemStream: TCLMemoryStream;
void SaveFile;
{
  try
    MemStream = TCLMemoryStream.Create;
    Image1.Bitmap.SaveToStream(MemStream);
    // Save image to file
    Clomosy.Base64ToFile(clPathCombine('newImgBG.png',Clomosy.AppFilesPath),MemStream.AsBase64);
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
}
{
  Form1 = TclForm.Create(self);
 
  Image1 = Form1.AddNewImage(Form1,'Image1');
  Image1.Align = alClient;
  Form1.SetImage(Image1,'https://clomosy.com/educa/bg2.png');
  Form1.AddNewEvent(Image1,tbeOnClick,'SaveFile');
 
  Form1.Run;
}
</pre>
:'''Base Syntax'''
<pre>
var
  Form1: TclForm;
  Image1: TclImage;
  MemStream: TCLMemoryStream;
procedure SaveFile;
begin
    try
      MemStream := TCLMemoryStream.Create;
      Image1.Bitmap.SaveToStream(MemStream);
      // Save image to file
      Clomosy.Base64ToFile(clPathCombine('newImgBG.png',Clomosy.AppFilesPath),MemStream.AsBase64);
    except
      ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
    end;
end;
begin
  Form1 := TclForm.Create(self);
 
    Image1 := Form1.AddNewImage(Form1,'Image1');
    Image1.Align := alClient;
    Form1.SetImage(Image1,'https://clomosy.com/educa/bg2.png');
    Form1.AddNewEvent(Image1,tbeOnClick,'SaveFile');
   
    Form1.Run;
end;
end;
</pre>
</pre>

Revision as of 10:27, 5 August 2024

If you want to add an image within the application, you can use the "TclImage" component. This component is defined as follows.

AddNewImage (TComponent, xName): TClImage

TComponent : The variable name of the defined component is written. Here you should write the component variable name of whatever your component will be in.

xName : The name of the defined image should be written.


Feature Use of Definition
TclImage testImg : TclImage; A variable belonging to the TclImage class is created.
AddNewImage testImg:= MyForm.AddNewImage(MyForm,'testImg'); A new Image is added to the form.
setImage MyForm.setImage(testImg,'https://clomosy.com/demos/bg.png'); It is used if you want to add an image to the Image object.
Clear testImg.MultiResBitmap.Clear; The Clear method clears images at all resolution levels within a MultiResBitmap property. So, when this method is called, all the images in the MultiResBitmap are deleted and rendered to an empty state.
Width testImg.Width := 150; Allows adjusting the width of the image.
Height testImg.Height := 50; Allows adjusting the height of the image.
Align testImg.Align := alTop; With the Align parameter, you can specify where you want our component to be aligned in the form. This parameter has multiple positioning properties. See the page to learn about these features.
Margins testImg.Margins.Left:= 50; // Right, Top, Bottom With the Margins parameter, you can give margins at any scale from the right, left, bottom, top.
TclImageMargins.png
LoadFromFile testImg.Bitmap.LoadFromFile('C:\Users\Administrator\Desktop\logo.png') The LoadFromFile method in Clomosy is used to load an image file and transfer it to a bitmap object. In the example, the expression testImg.Bitmap.LoadFromFile loads an image from a file path into the Bitmap property of a component named testImg.
SaveToStream testImg.Bitmap.SaveToStream(FileStream); //FileStream:TCLFileStream; Bitmap image (TclImage) transfer to TCLFileStream. See the page to learn about these features.
LoadFromStream testImg.Bitmap.LoadFromStream(FileStream); //FileStream:TCLFileStream; Transferring TCLFileStream data to bitmap image (TclImage). See the page to learn about these features.


Example 1:

TRObject Syntax
var
 MyForm:TclForm;
 testImg : TclImage;

{
 MyForm = TclForm.Create(Self);
 testImg= MyForm.AddNewImage(MyForm,'testImg');
 testImg.Align = alCenter;
 testImg.Height = 250;
 testImg.Width = 300;

 MyForm.setImage(testImg,'https://clomosy.com/demos/bg.png');
 MyForm.AddNewEvent(testImg,tbeOnClick,'ShowMessage(''Picture clicked.'');');
 MyForm.Run;

}
Base Syntax
var
MyForm:TclForm;
testImg : TclImage;
begin MyForm := TclForm.Create(Self); testImg:= MyForm.AddNewImage(MyForm,'testImg'); testImg.Align := alCenter; testImg.Height := 250; testImg.Width := 300;
MyForm.setImage(testImg,'https://clomosy.com/demos/bg.png'); MyForm.AddNewEvent(testImg,tbeOnClick,'ShowMessage(''Picture clicked.'');'); MyForm.Run;
end;


Output:
Image.png


Example 2(LoadFromFile):
In the example, a TclForm form is created and an image is downloaded from the internet to display on this form. First, a new TclForm instance named Form1 is created. The Form1.AddAssetFromUrl method is used to download an image from the specified URL to the project’s file directory. Then, a new TclImage instance is added to the form and configured to cover the entire form. The path of the downloaded image file is obtained using Clomosy.AppFilesPath and the Image1.Bitmap.LoadFromFile method is used to load the image.

TRObject Syntax
var
  Form1: TclForm;
  Image1: TclImage;

{
  Form1 = TclForm.Create(self);
  Form1.AddAssetFromUrl('https://clomosy.com/educa/bg2.png');
  try
    Image1 = Form1.AddNewImage(Form1,'Image1');
    Image1.Align = alClient;
    Image1.Bitmap.LoadFromFile(Clomosy.AppFilesPath+'bg2.png'); // Load image from file
    Form1.Run;
  except
    ShowMessage('An error was encountered while uploading the file.');
  }
}
Base Syntax
var
  Form1: TclForm;
  Image1: TclImage;

begin
  Form1 := TclForm.Create(self);
  Form1.AddAssetFromUrl('https://clomosy.com/educa/bg2.png');
  try
    Image1 := Form1.AddNewImage(Form1,'Image1');
    Image1.Align := alClient;
    Image1.Bitmap.LoadFromFile(Clomosy.AppFilesPath+'bg2.png'); // Load image from file
    Form1.Run;
  except
    ShowMessage('An error was encountered while uploading the file.');
  end;
end;


Example 3 (Save image to file) :
The example creates a form (TclForm) and an image component (TclImage) and implements a function to load an image from a specific URL and save it to a file. First, Form1 and Image1 objects are created, and the image component is added to the form. The Image1 component is updated with a background image from the specified URL (https://clomosy.com/educa/bg2.png). The SaveFile procedure uses TCLMemoryStream to save the bitmap of Image1 to a memory stream and writes this data to a file (newImgBG.png) in Base64 format. The SaveFile procedure is linked to the click event of Image1, so when the form is run and the user clicks the image, the image is saved to the file. If any errors occur, an error message is displayed.

TRObject Syntax
var
  Form1: TclForm;
  Image1: TclImage;
  MemStream: TCLMemoryStream;

void SaveFile;
{
  try
    MemStream = TCLMemoryStream.Create;
    Image1.Bitmap.SaveToStream(MemStream);
    // Save image to file
    Clomosy.Base64ToFile(clPathCombine('newImgBG.png',Clomosy.AppFilesPath),MemStream.AsBase64);
  except
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
}
{
  Form1 = TclForm.Create(self);
  
  Image1 = Form1.AddNewImage(Form1,'Image1');
  Image1.Align = alClient;
  Form1.SetImage(Image1,'https://clomosy.com/educa/bg2.png');
  Form1.AddNewEvent(Image1,tbeOnClick,'SaveFile');
  
  Form1.Run;
}
Base Syntax
var
  Form1: TclForm;
  Image1: TclImage;
  MemStream: TCLMemoryStream;

procedure SaveFile;
begin
    try
      MemStream := TCLMemoryStream.Create;
      Image1.Bitmap.SaveToStream(MemStream);
      // Save image to file
      Clomosy.Base64ToFile(clPathCombine('newImgBG.png',Clomosy.AppFilesPath),MemStream.AsBase64);
    except
      ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
    end;
end;
begin
  Form1 := TclForm.Create(self);
  
    Image1 := Form1.AddNewImage(Form1,'Image1');
    Image1.Align := alClient;
    Form1.SetImage(Image1,'https://clomosy.com/educa/bg2.png');
    Form1.AddNewEvent(Image1,tbeOnClick,'SaveFile');
    
    Form1.Run;
end;