Documentation > CMS Template API Library > Asset > CreateThumbnail(Img,String,Int32,Int32,Int32,Boolean)

CreateThumbnail

Creates a thumbnail of the specified size from the currently loaded image. A ThumbnailKey is required. The resulting image will be stored on the asset unless we are in upload.aspx, in that case it will be saved to the asset or current panel at the time the input form is saved. To change the currently uploaded image in place use the uploadContext.UploadedFile.UploadKey as the thumbnail Key. Important note, if calling from upload.aspx and the thumbnail key is not the same as the upload, make sure that the thumbnail key is in the list of strings passed as the hiddenFields parameter of the Input.ShowAcquireImage() call. Note: Due to memory limitations, the pixel count of the source image cannot be more than 10 million.

public CrownPeak.CMSAPI.ImgResult CreateThumbnail(Img,String,Int32,Int32,Int32,Boolean)


Returns

An ImgResult.

Parameters

NameDescriptionType
img The image to be used to create the thumbnail CrownPeak.CMSAPI.Img
thumbnailKey The Key used to refer to the thumbnail later as part of the asset's content. System.String
width The width. System.Int32
height The height. System.Int32
qualityByPercent Optional: The Quality of the saved JPG as a percent. Higher numbers have higher quality, but also bigger sizes. Defaults to 75. System.Int32
preserveAspectRatio Optional: Pass false if you want the thumbnail to be generated with exactly the size of the passed in width and height. Defaults to true. System.Boolean

Code Example

C#

Sample:
// Usually called from upload.aspx
UploadedFile imageUpload = context.UploadedFile;
string value = imageUpload.Path;

// my_photo_thumbnail was used in input.aspx because we want to display the thumbnail when we edit, not the possibly large original
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(value);

  if (!img.HasError)
  {
    ImgResult res = asset.CreateThumbnail(img, "my_photo_thumbnail", 300, 300);
    if (res.HasError)
    {
      context.Error = "Upload Error creating thumbnail on " + value + " error: " + res.ErrorMessage;
    }
  }
  else
  {
    context.Error = "Upload Error Loading on " + value + " error: " + img.ErrorMessage;
  }
 }      

Connect with Crownpeak