You can use Global events for MediaFileInfo object. If you bind it to Insert action that your method will be executed whenever a new media file is created (uploaded) in Kentico.
This global event may look like:
MediaFileInfo.TYPEINFO.Events.Insert.After += Insert_After;
and:
private void Insert_After1(object sender, ObjectEventArgs e)
{
var mediaFile = e.Object as MediaFileInfo;
if (mediaFile != null)
{
// get path to media file
var filePath = mediaFile.FilePath;
// check if it is jpg or one of your supported file types...
if (mediaFile.FileExtension.Equals("jpg", StringComparison.OrdinalIgnoreCase))
{
// Resize and save image here
}
}
}
In the past I've used following code to resize an image:
int height = 50;
int width = 50;
string filePath = Server.MapPath("~") + "\\SomeFolder\\OriginalPicture.png";
string newfilePath = Server.MapPath("~") + "\\SomeFolder\\ResizedPicture.png";
// load image
var imageHelper = new ImageHelper(CMS.IO.File.ReadAllBytes(filePath));
// resize image
var resizedImage = imageHelper.GetResizedImage(width, height);
// save image
resizedImage.Save(newfilePath);