Kentico 11: How to get Width and Height from image?

Vuthy Sok asked on October 31, 2018 17:42

Hi All,

I have create custom WebPart for display image list. For Image filed I am used DataType: Text and Control Form: Media Selection.

I want to get Width and Height from Image that i have uploaded.

Could you please advise me what I need to do for solving this problem well?

Regards

Vuthy

Correct Answer

David te Kloese answered on November 1, 2018 13:27

Check the API examples at docs.kentico.com/...#Medialibraries-Mediafoldersandfiles

If you have the item GUID you'd do something like:

CMS.MediaLibrary.MediaFileInfo yourfile = CMS.MediaLibrary.MediaFileInfoProvider.GetMediaFileInfo(fileGuid, SiteContext.CurrentSiteName);

or if you just have the path: (you might need to remove the ~ or first /.)

CMS.MediaLibrary.MediaFileInfo yourfile = CMS.MediaLibrary.MediaFileInfoProvider.GetMediaFiles().Where("FilePath", yourMediaPath).FirstObject();

The media file itself has the following properties:

    var imgHeight = yourfile.FileImageHeight;
    var imgWidth = yourfile.FileImageWidth;
3 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on November 1, 2018 11:19

Hi,
In your custom web part, you can access the media file object from the media selection field - e.g. by its GUID or path. Then, this media file object has the height and width properties you could use.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.