Documentation > CMS Template API Library > Img > CreateThumbnail(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 in memory. Use Asset.CreateThumbnail, PanelEntry.CreateThumbnail, or ImageForm.CreateThumbnail to preserve the data. Note: Due to memory limitations, the pixel count of the source image cannot be more than 10 million.
public CrownPeak.CMSAPI.ImgResult CreateThumbnail(String,Int32,Int32,Int32,Boolean)
Parameters
Name | Description | Type |
---|---|---|
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. | 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. | 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 = img.CreateThumbnail("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; } }