Documentation > CMS Template API Library > Img > Load(UploadedFile)

Load

Loads an image using an uploaded file.

public CrownPeak.CMSAPI.Img Load(UploadedFile)


Returns

The Img object.

Parameters

NameDescriptionType
file The file: a file that has been uploaded into the CMS. CrownPeak.CMSAPI.UploadedFile

Code Example

C#

Sample:
 
            
            
            // Load an image from an uploaded attachment
            UploadedFile file = asset.UploadedFiles["my_photo_uploaded"];
            if (file.IsLoaded)
            {
              Img img = Img.Load(file);
              if (!img.HasError)
              {
                Out.WriteLine("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" />", file, img.Width, img.Height);
              }
              else
              {
                Out.WriteLine("Loading image failed. Error: {0}", img.ErrorMessage);
              }
            }
            
            // Handling an Upload in upload.aspx
            // This property is defined in the UploadContext which is available while running upload.aspx
            UploadedFile imageUpload = context.UploadedFile;
            // Is this the file called "my_photo_thumbnail"?
            if (imageUpload.Matches("my_photo_thumbnail"))
            {
              asset.SaveUploadedFile("my_photo", imageUpload); // it gets overwritten later when it is resized to 300x300
              Img img = Img.Load(imageUpload);
              if (!img.HasError)
              {
                ImgResult res = img.CreateThumbnail("my_photo_thumbnail", 300, 300);
                if (res.HasError)
                {
                  context.Error = "Upload Error creating thumbnail on " + imageUpload + " error: " + res.ErrorMessage;
                }
              }
              else
              {
                context.Error = "Upload Error Loading on " + imageUpload + " error: " + img.ErrorMessage;
              }
            }
            
            
            

Connect with Crownpeak