【发布时间】:2018-08-13 14:55:54
【问题描述】:
我正在寻找一种通过 Umbraco 事件获取图像宽度的方法,以在上传之前验证图像是否符合特定的纵横比。问题是我不知道是否可以将 mediaItem 转换为 Image。到目前为止,如何获取宽度或高度是我的事件代码:
public class ImageResizer : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += MediaServiceSaving;
}
void MediaServiceSaving(IMediaService sender, SaveEventArgs<IMedia> e)
{
foreach (var mediaItem in e.SavedEntities)
{
Image img = (Image)mediaItem;
double height = img.Height;
double width = img.Width;
string msg = "" + height;
if (height / width != 1)
{
msg = "Ratio is not 1:1. Please make sure the width and height of your image is the same.";
BasePage.Current.ClientTools.ShowSpeechBubble(BasePage.speechBubbleIcon.success, "Error", msg);
}
else
{
//Ducplicate image to images and thubnails of desired choice
//Send images to database - continue normal process
}
}
}
}
【问题讨论】:
标签: c# asp.net-mvc image umbraco umbraco7