相关文章推荐
憨厚的长颈鹿  ·  Android ...·  2 年前    · 
腼腆的人字拖  ·  Oracle ...·  2 年前    · 
痴情的豆腐  ·  oracle update语句 ...·  2 年前    · 
火星上的小熊猫  ·  scRNA-Seq | SCENIC ...·  2 年前    · 
憨厚的墨镜  ·  【SEED Labs ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Hi I'm currently using Delphi 2010. I basically have a form where a user has to enter information about themselves and upload a picture. I have an Image component on my form. I did some research and many of the websites I looked at said to use a OpenPictureDialogue to allow the user to select an image and display it in the Image component.

My question is, how can I save this image to a file on my computer? Keeping in mind I will have multiple users adding their picture and that I will have to use the picture later on again, basically I want to use the LoadFromFile procedure to display the picture in my program later on.

I also read many websites saying to use the SavePictureDialogue, but that allows the user to select the file they want the image to be saved to and I don't want that, I want it to save to a file that only I can access. I have this so far, I know it is very limited.

 if opdAcc.Execute then
   begin
     if opdAcc.FileName <> '' then
       begin
         imgAccImage.Picture.LoadFromFile(opdAcc.FileName);

I am a student and my knowledge is quite limited and I would appreciate any help. :)

"how can I save this image to a file on my computer?" - it is already a file on the computer. TOpen(Picture)Dialog is meant for the user to select an existing file on the computer and return its path as a string, which the application can then use as needed, such as loading the file into your TImage component. So, I'm not really understanding what your problem is. Just keep track of the file path that you use to load the image, so you can load it again and again when needed. – Remy Lebeau Jul 31, 2020 at 4:30 If that is not an option, then simply call imgAccImage.Picture.Graphic.SaveToFile() to save the image to whatever file path you want. – Remy Lebeau Jul 31, 2020 at 4:33 @RemyLebeau You can use TopenDialog to open a file from removable or network drive which means that only storing the file location might not be good enough since that location might not be accessible in the future. So making a copy of said image is necessary. – SilverWarior Aug 1, 2020 at 7:35 I'm more interested on what data structure does OP use in order to store its user information. Because if it is a database then it might be better option to go and store this image in database itself along other user information. – SilverWarior Aug 1, 2020 at 7:40 @JadeBrummer use a blob field. Save the image to that field rather than to a file. See TDataSet.CreateBlobStream() and TGraphic.(SaveTo|LoadFrom)Stream() – Remy Lebeau Aug 1, 2020 at 17:34

First of all, there is no place on the hard drive that only you can access. But you can create a folder to store your files and copy users' pictures there. This reduces the likelihood that the user will have access to these files. The usual folder for storing such files is the AppData folder. It is better to create a folder with the same name as your application in AppData and store such files there.

Suppose the GetPicturesDirectoryPath function generates the address of such a folder and ensures that this folder has already been created or will be created. The next step is to generate a unique name for the file you want to store. Note that multiple users may select files with the same name. In this case, after copying the picture selected by the second user, the image file will be written over the previous user's file. If a unique identifier is assigned to each user, this identifier is the best choice for the picture file name. But you can use the GetGUIDFileName function to create a unique address. Make sure the generated address is kept with the rest of the user information, or the connection between the copied file and the user will be lost. The implementation of all these will be something like the following:

uses IOUtils;
function GetAppDataDirectoryPath: string;
begin
function GetPicturesDirectoryPath: string;
begin
  Result := TPath.Combine(GetAppDataDirectoryPath, 'MyApp');
  TDirectory.CreateDirectory(Result);
function GetUniqueFilePath(const Extension: string): string;
begin
  Result := TPath.ChangeExtension(
    TPath.Combine(GetPicturesDirectoryPath, TPath.GetGUIDFileName),
    Extension);
procedure TForm1.Button1Click(Sender: TObject);
  DestPath: string;
begin
  OpenPictureDialog1.Options := OpenPictureDialog1.Options +
    [ofFileMustExist]; // make sure that selected file exists
  if OpenPictureDialog1.Execute then
  begin
    DestPath := GetUniqueFilePath(TPath.GetExtension(OpenPictureDialog1.FileName));
    TFile.Copy(OpenPictureDialog1.FileName, DestPath);
    if TFile.Exists(DestPath) then
      Image1.Picture.LoadFromFile(DestPath)
      ShowMessage('Well, something went wrong!');

Read this to implement GetAppDataDirectoryPath.

There is a problem with your approach. And that is if user click button for choosing image multiple times you would copy multiple image files to your application folder even thou in the end only one would be used. Instead I would just temporarily store original image location and make a copy of the said image only when user is done entering his information. This is usually done on closing of form designed for entering user data. Also once file location is stored in user data record it should be reused so that if in future user decides to change his image the original one will get overwritten – SilverWarior Aug 1, 2020 at 7:46 @saastn Hi thanks for the help, appreciate it. Each user basically has a unique ID that is stored in a string variable so how would I link the file path to that ID? – Jade Brummer Aug 1, 2020 at 10:13 @Jade, if you are planning to use user ID as file name, so there is nothing more to do. Next time you needed to retrieve user's picture, use their ID to reconstruct the path. But if for some reason you decided to generate a unique file name, like by calling GetGUIDFileName, then you need to store the file name along with other information associated with the user. For example, add a new column to the Users table and name it PictureFileName. – saastn Aug 1, 2020 at 15:58 But when you are already using a database, store the image itself in the database. When you want to store the original file, it’s best to add the filename (or at least the file extension/type - like jpeg, bnp, gif) so you know which image type it is. – R. Hoek Aug 2, 2020 at 21:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.